Vortex.ihrc.fiu.edu



Matplotlib-pyplotmatplotlib.pyplot is a collection of command style functions that make matplotlib work like MATLAB when making figures.To use ‘matplotlib’ graphics package, you need to do, >>>import matplotlib.pyplot as plt>>>dir(plt)Will show all the commands that ‘Matplotlib.pyplot’ has. In this class, we cannot cover all these commands, if you want to learn them by yourself, you can do,>>>help(plt.contour)….**Examples:** .. plot:: mpl_examples/pylab_examples/contour_demo.py .. plot:: mpl_examples/pylab_examples/contourf_demo.py .. plot:: mpl_examples/pylab_examples/contour_corner_mask.pyOr you can visit . You can find ‘mpl_examples’ in ‘docs \ Matplotlib Examples\ pylab_examples Examples.1. ‘pyplot.plot’Example: plt.plot_example1.pyimport matplotlib.pyplot as pltimport numpy as npx=np.arange(0,7,0.1)y=np.sin(x)plt.plot(x,y)plt.show()how to control curve properties#plt.plot(x,y,'--')#plt.plot(x,y,'-.')#plt.plot(x,y,':')#plt.plot(x,y,'b')#plt.plot(x,y,'r')#plt.plot(x,y,'g')#plt.plot(x,y,'y')#plt.plot(x,y,'c')#plt.plot(x,y,'m')#plt.plot(x,y,'w')#plt.plot(x,y,'k')#plt.plot(x,y,'.')#plt.plot(x,y,'.r')#plt.plot(x,y,'*')#plt.plot(x,y,'o')#plt.plot(x,y,'p')#plt.plot(x,y,'d')#plt.plot(x,y,'>')#plt.plot(x,y,'<')#plt.plot(x,y,'x')#plt.plot(x,y,'+')#plt.plot(x,y,'s')#plt.plot(x,y,'v')#plt.plot(x,y,'^')#plt.plot(x,y,'h')plt.show()how to change line width?>>> plt.plot(x,y, linewidth=5.0)how to define your own color?>>> plt.plot(x,y, color=[0.5,0.5,0.5])how to change marker size?>>> plt.plot(x,y, 'o', markersize=20.0)how to add labels and title?>>> plt.xlabel('some numbers')>>> plt.ylabel('some numbers')>>> plt.title('Illustration plot')how to change font size of labels and title?>>> plt.xlabel('some numbers',fontsize=28)>>> plt.ylabel('some numbers',fontsize=28)>>> plt.title('Illustration plot',fontsize=28)how to change label ticks?>>> xticks=np.arange(0,7.5,0.5)>>> plt.xticks(xticks)>>> yticks=np.arange(-1,1.25,0.25)>>> plt.yticks(yticks)>>> plt.xticks(xticks,['a','b','c','d','e','f','g','h'])>>> plt.xticks(xticks,[])>>> labels = ['Frogs', 'Hogs', 'Bogs', 'Slogs','Frogs', 'Hogs', 'Bogs', 'Slogs']>>> plt.xticks(xticks,labels, rotation='vertical')how to change axis range>>> plt.axis([0, 6, 0, 20])how to put grid on>>> plt.grid(True)1.10 how to plot multiple curves>>> x=np.arange(0,7,0.1)>>> y=np.sin(x)>>> z=np.cos(x)>>> plt.plot(x,y)>>> plt.plot(x,z,'r')>>> plt.plot(x,y,x,z,'r')1.11 how to add legend>>> plt.legend(['sin','cos'])>>> plt.legend(['sin','cos'],loc=1) #loc controls the location of the legend.>>> plt.legend(['sin','cos'],loc=1,fontsize=20) # change location and font size of the legend.1.12 how to add text>>> plt.text(5,0.5,'text here') >>> plt.text(3,0.5,'text here',fontsize=20)>>> plt.text(3,0.5,'text here',fontsize=20,color='r')>>> plt.text(3,0.5,'text here',style='italic')>>> plt.text(3,0.5,'text here',fontweight='bold')>>> plt.text(3,0.5,'text here',bbox={'facecolor':'red', 'alpha':0.1, 'pad':10}) #alpha and pad control depth of shapes and pad size.>>> plt.text(5,0.5,'$E=cm^2$') # write equations>>> plt.text(5,0.5,r'$\alpha +\beta$')>>> plt.text(5,0.5,r'$\sum_{n=1}^\infty\frac{-e^{i\pi}}{2^n}\frac{\partial u}{\partial t}$', fontsize=16, color='gray')1.13 how to add arrow>>> plt.arrow(0, -0.5, 1.2, 0.3, head_width=0.05, head_length=0.2, fc='k', ec='k')1.14 how to save a figure to a file>>> plt.savefig('plt.plot_example1.pdf')>>> plt.savefig('plt.plot_example1.png')1.15 how to make multiple plotsExample: plt.plot_example2.py# one panel>>> import numpy as np>>> import matplotlib.pyplot as plt>>> x = np.linspace(0, 2 * np.pi, 400)>>> y = np.sin(x ** 2)>>> f, ax =plt.subplots()>>> ax.plot(x,y)>>> ax.set_title('One panel')>>> plt.savefig('plt.plot_example2.pdf')>>> plt.savefig('plt.plot_example2.png')>>> plt.show()# two panel>>> f, (ax1, ax2) =plt.subplots(1,2)>>> ax1.plot(x,y)>>> ax1.set_title('Panel one')>>> ax2.plot(x,y,'ro')>>> ax2.set_title('Panel two')# four panel>>> f, ((ax1, ax2),(ax3, ax4)) =plt.subplots(2,2)>>> #f, ((ax1, ax2),(ax3, ax4)) =plt.subplots(2,2,sharex='col',sharey='row')>>> ax1.plot(x,y)>>> ax1.set_title('Panel one')>>> ax1.set_xlabel('Time')>>> ax1.set_ylabel('Amplitude')>>> ax2.plot(x,y,'ro')>>> ax2.set_title('Panel two')>>> ax2.set_xlabel('Time')>>> ax2.set_ylabel('Amplitude')>>> ax3.plot(x,y**2,'m*')>>> ax3.set_title('Panel three')>>> ax3.set_xlabel('Time')>>> ax3.set_ylabel('Amplitude')>>> ax4.plot(x,y**3,'k>')>>> ax4.set_title('Panel four')>>> ax4.set_xlabel('Time')>>> ax4.set_ylabel('Amplitude')# another way>>> f, ax =plt.subplots(2,2)>>> ax[0,0].plot(x,y)>>> ax[0,0].set_title('Panel one')>>> ax[0,0].set_xlabel('Time')>>> ax[0,0].set_ylabel('Amplitude')>>> ax[0,1].plot(x,y,'ro')>>> ax[0,1].set_title('Panel two')>>> ax[0,1].set_xlabel('Time')>>> ax[0,1].set_ylabel('Amplitude')>>> ax[1,0].plot(x,y**2,'m*')>>> ax[1,0].set_title('Panel three')>>> ax[1,0].set_xlabel('Time')>>> ax[1,0].set_ylabel('Amplitude')>>> ax[1,1].plot(x,y**3,'k>')>>> ax[1,1].set_title('Panel four')>>> ax[1,1].set_xlabel('Time')>>> ax[1,1].set_ylabel('Amplitude')# how to make a good layout>>> plt.tight_layout()>>> plt.tight_layout(pad=3)>>> plt.tight_layout(h_pad=2.5, w_pad=0.5)>>> plt.tight_layout(rect=[0.5, 0, 1, 1])# another way>>> ax1 = plt.subplot2grid((2,2), (0,0), colspan=1)>>> ax1.plot(x,y)>>> ax1.set_title('Panel one')>>> ax1.set_xlabel('Time')>>> ax1.set_ylabel('Amplitude')>>> ax2 = plt.subplot2grid((2,2), (0,1), colspan=1)>>> ax2.plot(x,y)>>> ax2.set_title('Panel one')>>> ax2.set_xlabel('Time')>>> ax2.set_ylabel('Amplitude')>>> ax3 = plt.subplot2grid((2,2), (1,0), colspan=1)>>> ax3.plot(x,y**2,'m*')>>> ax3.set_title('Panel three')>>> ax3.set_xlabel('Time')>>> ax3.set_ylabel('Amplitude')>>> ax4 = plt.subplot2grid((2,2), (1,1), colspan=1)>>> ax4.plot(x,y**3,'k>')>>> ax4.set_title('Panel four')>>> ax4.set_xlabel('Time')>>> ax4.set_ylabel('Amplitude')# another way>>> import matplotlib.gridspec as gridspec>>> gs1 = gridspec.GridSpec(2, 2)>>> gs1.update(left=0.1, right=0.97, bottom=0.09, top=0.95, wspace=0.3, hspace=0.35)>>> ax1 = plt.subplot(gs1[0, 0])>>> ax1.plot(x,y)>>> ax1.set_title('Panel one')>>> ax1.set_xlabel('Time')>>> ax1.set_ylabel('Amplitude')>>> gs1.update(left=0.1, right=0.97, bottom=0.09, top=0.95, wspace=0.3, hspace=0.35)>>> ax2 = plt.subplot(gs1[0, 1])>>> ax2.plot(x,y,'ro')>>> ax2.set_title('Panel one')>>> ax2.set_xlabel('Time')>>> ax2.set_ylabel('Amplitude')>>> gs1.update(left=0.1, right=0.97, bottom=0.09, top=0.95, wspace=0.3, hspace=0.35)>>> ax3 = plt.subplot(gs1[1, 0])>>> ax3.plot(x,y,'ro')>>> ax3.set_title('Panel one')>>> ax3.set_xlabel('Time')>>> ax3.set_ylabel('Amplitude')>>> gs1.update(left=0.1, right=0.97,bottom=0.09, top=0.95, wspace=0.3, hspace=0.35)>>> ax4 = plt.subplot(gs1[1, 1])>>> ax4.plot(x,y,'ro')>>> ax4.set_title('Panel one')>>> ax4.set_xlabel('Time')>>> ax4.set_ylabel('Amplitude')# how to make multiple figures>>> import matplotlib.pyplot as plt>>> import numpy as np>>> t = np.arange(0.0, 2.0, 0.01)>>> s1 = np.sin(2*np.pi*t)>>> s2 = np.sin(4*np.pi*t)>>> plt.figure(1)>>> plt.subplot(211)>>> plt.plot(t, s1)>>> plt.subplot(212)>>> plt.plot(t, 2*s1)>>> plt.figure(2)>>> plt.plot(t, s2)# now switch back to figure 1 and make some changes#plt.figure(1)#plt.subplot(211)#plt.plot(t, s2, 'gs')#ax = plt.gca()#ax.set_xticklabels([])>>> plt.show() ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download