pylab[1]

import pylab

1
2
3
4
5
6
7
import pylab
pylab.figure(figsize=(10,9))
pylab.plot(x,y,'-o',markersize=20)
pylab.legend([leg1,])
pylab.xlabel('x(m)')
pylab.ylabel('y(m)')
pylab.show()

display latex formula in xlabel, ylabel[2]

1
2
pylab.xlabel(r'$latex symbols$')
pylab.legend(['$latex \pi=x^2$',])

display only one marker point in legend [3]

1
2
from pylab import *
rcParams['legend.numpoints'] = 1

text annotation [4]

1
pylab.text(rho_LFM,Te_LFM/1000+0.01,LFM_str,fontsize=15)

set default fontsize for all figure elements

1
2
3
import pylab
from pylab import *
rcParams.update({'font.size':15})

tight layout if the text or edges of the figure are covered

1
fig.tight_layout()

turn grid and minorticks on

1
2
3
4
5
pylab.grid('on')
pylab.minorticks_on()
# adjust the marker size of major and minor label ticks
pylab.tick_params(which='major',labelsize=15,width=2,length=10,color='black')
pylab.tick_params(which='minor',width=1,length=5)

colormaps in pylab [5]

In python pylab module, you can choose many colormaps. Such as: ‘jet’, ‘hsv’, ‘pink’, ‘copper’, etc.

set axis as log scale [6]

1
pylab.yscale('log')

line and marker style setting

set marker size [7]

1
pylab.plot(S,'o',markersize=10)

set tick and label value formate [9]

1
2
3
4
5
6
7
8
9
10
11
12
13
from matplotlib.ticker import FormatStrFormatter
matplotlib.pyplot.plot(x1, y1)
ax = matplotlib.pyplot.gca()
# set x axis as float format with 2 order after decimal point
ax.xaxis.set_major_formatter(FormatStrFormatter('%.2f'))
3 set y axis as round format
ax.yaxis.set_major_formatter(FormatStrFormatter('%d'))
# set x axis as scitific formate for 1e-9
ax.yaxis.set_major_formatter(FuncFormatter(lambda x1, pos: ('%.1f')%(x1*1e9)))
ax.set_ylabel('microseconds (1E-9)')
# other method to set tick format
# ax.plt.ticklabel_format(axis='x', style='sci', scilimits=(-1,1))
# plt.ticklabel_format(axis='x', style='plain')

set colorbar tick format [10]

1
matplotlib.pyplot.colorbar(format = '%.1f')

subplot

multiple subplot

The combination process of multiple subplot can be understood with the help of the above figure.

1
2
ax1 = plt.subplot(2, 1, 1)
ax2 = plt.subplot(2, 1, 2, sharex = ax1, sharey = ax1)

contour plot

contourf

1
2
3
4
5
6
pylab.figure()
pylab.contourf(TP_zoom, PHI_zoom*180/pi, T_probes_stand_zoom, interpolation = 'gaussian')
pylab.xlim([t[0], t[0] + 8*1.0/f_MHD])
pylab.xlabel('t (s)')
pylab.ylabel('phi (degree)')
pylab.title('n = '+str(n)+', f = '+str(f_MHD/1000)+' kHz')

This is a simple scirpt to show the toroidal mode structure of an MHD mode. The option interpolation = ‘gaussian’ controls whether to make smooth to the contourf plot, transformation between color region.

set colormap range [8]

1
2
3
4
5
6
7
Vmax = 0.01
Vmin = -0.01
new_range = numpy.linspace(Vmin, Vmax, 16)
plt.figure(figsize=[8,5])
# pylab.contourf(t_fit, tor_angle*180/pi, sig_fit_matrix.T, 50, cmap = 'jet')
plt.contourf(t_fit, tor_angle*180/pi, sig_fit_matrix.T, new_range, cmap = 'jet')
cbar = plt.colorbar()

The set of new_range is key to the limit of colormap, it will reset the colorbar within this range. The data value outside this range will be set to white on the colormap by as default. If this value is set as a scalar value N, it will dense the data by N times.

Reference

[1] https://stackoverflow.com/questions/14827650/pyplot-scatter-plot-marker-size

[2] https://stackoverflow.com/questions/2140865/using-the-symbol-font-for-greek-symbols-in-tex-via-matplotlib

[3] https://stackoverflow.com/questions/6146778/matplotlib-legend-markers-only-once

[4] https://stackoverflow.com/questions/8482588/putting-text-in-top-left-corner-of-matplotlib-plot

[5] https://matplotlib.org/users/colormaps.html

[6] https://stackoverflow.com/questions/773814/plot-logarithmic-axes-with-matplotlib-in-python

[7] https://stackoverflow.com/questions/14827650/pyplot-scatter-plot-marker-size

[8] https://stackoverflow.com/questions/21952100/setting-the-limits-on-a-colorbar-in-matplotlib

[9] https://stackoverflow.com/questions/3677368/matplotlib-format-axis-offset-values-to-whole-numbers-or-specific-number

[10] https://matplotlib.org/api/_as_gen/matplotlib.pyplot.colorbar.html

[11] https://stackoverflow.com/questions/14482422/link-axis-between-different-plot-no-subplots-using-matplotlib