Vortex.ihrc.fiu.edu



Two dimensional plots1. contour plotsExamples of contour plotsimport matplotlibimport numpy as npimport matplotlib.pyplot as pltdelta = 0.025x = np.arange(-3.0, 3.0, delta)y = np.arange(-2.0, 2.0, delta)X, Y = np.meshgrid(x, y)# The other method#import matplotlib.mlab as mlab#Z1 = mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)#Z2 = mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1)Z1 = plt.mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)Z2 = plt.mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1)Z = 10.0 * (Z2 - Z1)fig=plt.figure(1)plt.subplot(2,2,1)plt.plot(x,Z1[80,:])plt.plot(x,Z2[80,:],'r')plt.plot(x,Z[80,:],'g')plt.subplot(2,2,2)plt.plot(y,Z1[:,120])plt.plot(y,Z2[:,120],'r')plt.plot(y,Z[:,120],'g')fig=plt.figure(2)plt.subplot(2,2,1)CS = plt.contour(X, Y, Z)plt.clabel(CS, inline=1, fontsize=8)plt.title('Simplest default with labels')plt.subplot(2,2,2)CS = plt.contour(X, Y, Z)manual_locations = [(-1, -1.4), (-0.62, -0.7), (-2, 0.5), (1.7, 1.2), (2.0, 1.4), (2.4, 1.7)]plt.clabel(CS, inline=1, fontsize=8, manual=manual_locations)plt.title('labels at selected locations')plt.subplot(2,2,3)CS = plt.contour(X, Y, Z, 6, colors='k') # negative contours will be dashed by default )plt.clabel(CS, fontsize=8, inline=1)plt.title('Single color - negative contours dashed')plt.subplot(2,2,4)#matplotlib.rcParams['contour.negative_linestyle'] = 'solid'#CS = plt.contour(X, Y, Z, 6, colors='k')# using keyword linestyles = 'solid','dashed','dotted','dashdot'CS = plt.contour(X, Y, Z, 6, colors='k',linestyles='solid')plt.clabel(CS, fontsize=8, inline=1)plt.title('Single color - negative contours solid')fig=plt.figure(3)plt.subplot(2,2,1)CS = plt.contour(X, Y, Z, 12, linewidths=np.arange(.5, 4, .5), colors=('r', 'green', 'blue', (1, 1, 0), '#afeeee', '0.5') )plt.clabel(CS, fontsize=8, inline=1)plt.title('Crazy lines')plt.subplot(2,2,2)levels = np.arange(-1.0, 1.8, 0.2)CS = plt.contour(X, Y, Z, levels)plt.clabel(CS, fontsize=8, inline=1)plt.subplot(2,2,3)levels = [-1.0, -0.5, 0, 0.5, 1.0, 1.5, 2.0]linewidths=[1, 1, 5, 1, 1, 1, 1]CS = plt.contour(X, Y, Z, levels, linewidths=linewidths)plt.clabel(CS, fontsize=8, inline=1)plt.subplot(2,2,4)levels = [-1.0, -0.5, 0, 0.5, 1.0, 1.5, 2.0]linewidths=[3, 3, 5, 3, 3, 3, 3]CS = plt.contour(X, Y, Z, levels, linewidths=linewidths)plt.clabel(CS, fontsize=8, inline=1)CB = plt.colorbar(CS, shrink=0.8)plt.tight_layout()fig=plt.figure(4)plt.subplot(2,2,1)CS = plt.contour(Z,origin='lower')plt.clabel(CS, fontsize=8, inline=1)plt.subplot(2,2,2)CS = plt.contour(Z,origin='upper')plt.clabel(CS, fontsize=8, inline=1)plt.subplot(2,2,3)CS = plt.contour(Z,origin='lower',extent=(-3, 3, -2, 2))plt.clabel(CS, fontsize=8, inline=1)plt.subplot(2,2,4)CS = plt.contour(Z,origin='lower',extent=(-3, 3, -2, 2))plt.clabel(CS, levels[1::2], fontsize=8, inline=1)plt.show()2. contourf plotsExamples of contourf plotsimport matplotlibimport numpy as npimport matplotlib.mlab as mlabimport matplotlib.pyplot as pltdelta = 0.025x = np.arange(-3.0, 3.0, delta)y = np.arange(-2.0, 2.0, delta)X, Y = np.meshgrid(x, y)Z1 = mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)Z2 = mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1)Z = 10.0 * (Z2 - Z1)fig=plt.figure(1)plt.subplot(2,2,1)CS = plt.contourf(X, Y, Z)plt.subplot(2,2,2)CS = plt.contourf(X, Y, Z,5)plt.subplot(2,2,3)CS = plt.contourf(X, Y, Z,10)plt.subplot(2,2,4)CS = plt.contourf(X, Y, Z,5, alpha=0.5)fig=plt.figure(2)plt.subplot(2,2,1)levels=np.arange(-1.6, 2.0, 0.2)CS = plt.contourf(X, Y, Z, levels)plt.subplot(2,2,2)levels=np.arange(-1.6, 2.0, 0.2)CS = plt.contourf(X, Y, Z, levels)CS1 = plt.contour(X, Y, Z, levels,colors='k')plt.subplot(2,2,3)levels=np.arange(-1.6, 2.0, 0.2)CS = plt.contourf(X, Y, Z, levels)CS1 = plt.contour(X, Y, Z, levels,colors='k',linestyles='solid')plt.subplot(2,2,4)levels=np.arange(-1.6, 2.0, 0.2)CS = plt.contourf(X, Y, Z, levels)CS1 = plt.contour(X, Y, Z, levels=CS.levels[::2],colors='k',linestyles='dotted')fig=plt.figure(3)plt.subplot(2,2,1)levels=np.arange(-1.6, 2.0, 0.2)CS = plt.contourf(X, Y, Z, levels,cmap=plt.cm.bone)plt.subplot(2,2,2)levels=np.arange(-1.6, 2.0, 0.2)CS = plt.contourf(X, Y, Z, levels,cmap=plt.cm.PuOr)CB = plt.colorbar(CS, shrink=0.8)plt.subplot(2,2,3)levels=np.arange(-1.6, 2.0, 0.2)CS = plt.contourf(X, Y, Z, levels,cmap=plt.cm.Pastel1)CB = plt.colorbar(CS, shrink=0.8,orientation='horizontal') #verticalplt.subplot(2,2,4)levels=np.arange(-1.6, 2.0, 0.2)CS = plt.contourf(X, Y, Z, levels,cmap=plt.cm.cmapname)CB = plt.colorbar(CS, shrink=0.8,aspect=10) # default 20fig=plt.figure(4)plt.subplot(2,2,1)levels=np.arange(-1.6, 2.0, 0.2)CS = plt.contourf(X, Y, Z, levels,cmap=plt.cm.YlGnBu)CB = plt.colorbar(CS)plt.subplot(2,2,2)levels=np.arange(-1.6, 2.0, 0.2)CS = plt.contourf(X, Y, Z, levels,cmap=plt.cm.YlGnBu)CB = plt.colorbar(CS,ticks=levels)plt.subplot(2,2,3)levels=np.arange(-1.6, 2.0, 0.2)CS = plt.contourf(X, Y, Z, levels,cmap=plt.cm.YlGnBu)CB = plt.colorbar(CS,ticks=levels[::3])plt.subplot(2,2,4)levels=np.arange(-1.6, 2.0, 0.2)CS = plt.contourf(X, Y, Z, levels,cmap=plt.cm.YlGnBu)CB = plt.colorbar(CS,ticks=[-1.2,0,1.2])fig=plt.figure(5)plt.subplot(2,2,1)levels = np.arange(-1, 1.0, 0.2)CS3 = plt.contourf(X, Y, Z, levels=levels,extend='none')plt.subplot(2,2,2)levels = np.arange(-1, 1.0, 0.2)CS3 = plt.contourf(X, Y, Z, levels=levels,extend='both')plt.subplot(2,2,3)levels = np.arange(-1, 1.0, 0.2)CS3 = plt.contourf(X, Y, Z, levels=levels,extend='both')CS3.cmap.set_under(color='m')CS3.cmap.set_over('gray')CB = plt.colorbar(CS3, shrink=0.8) #it won't work without colorbarplt.subplot(2,2,4)levels = np.arange(-1, 1.0, 0.2)CS3 = plt.contourf(X, Y, Z, levels=levels,extend='min') #min, maxCS3.cmap.set_under(color='m')CS3.cmap.set_over('gray')CB = plt.colorbar(CS3, shrink=0.8) #it won't work without colorbarplt.show()3. imshow plotsExamples of imshow plotsimport matplotlibimport numpy as npimport matplotlib.mlab as mlabimport matplotlib.pyplot as plt#delta = 0.025delta = 0.5x = np.arange(-3.0, 3.0, delta)y = np.arange(-2.0, 2.0, delta)X, Y = np.meshgrid(x, y)Z1 = mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)Z2 = mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1)# difference of GaussiansZ = 10.0 * (Z2 - Z1)fig=plt.figure(1)plt.subplot(2,2,1)im = plt.imshow(Z,interpolation='none',cmap=plt.cm.jet)plt.subplot(2,2,2)im = plt.imshow(Z, origin='none',interpolation='none',cmap=plt.cm.jet)plt.subplot(2,2,3)im = plt.imshow(Z, origin='lower',interpolation='none',cmap=plt.cm.jet)plt.subplot(2,2,4)im = plt.imshow(Z, origin='upper',interpolation='none',cmap=plt.cm.jet)fig=plt.figure(2)plt.subplot(2,2,1)im = plt.imshow(Z, interpolation='none', origin='lower', cmap=plt.cm.jet, extent=(-3, 3, -2, 2))plt.subplot(2,2,2)im = plt.imshow(Z, interpolation='bilinear', origin='lower', cmap=plt.cm.jet, extent=(-3, 3, -2, 2))plt.subplot(2,2,3)im = plt.imshow(Z, interpolation='gaussian', origin='lower', cmap=plt.cm.jet, extent=(-3, 3, -2, 2))plt.subplot(2,2,4)im = plt.imshow(Z, interpolation='bicubic', origin='lower', cmap=plt.cm.jet, extent=(-3, 3, -2, 2))#interpolation : string, optional, default: None# Acceptable values are 'none', 'nearest', 'bilinear', 'bicubic',# 'spline16', 'spline36', 'hanning', 'hamming', 'hermite', 'kaiser',# 'quadric', 'catrom', 'gaussian', 'bessel', 'mitchell', 'sinc',# 'lanczos'plt.show()3. quiver plotsExamples of quiver plotsimport matplotlib.pyplot as pltimport numpy as npX, Y = np.meshgrid(np.arange(0, 2 * np.pi, .2), np.arange(0, 2 * np.pi, .2))U = np.cos(X)V = np.sin(Y)fig=plt.figure(1)Q = plt.quiver(U, V)qk = plt.quiverkey(Q, 0.5, 0.92, 2, r'$2 \frac{m}{s}$', labelpos='W', labelsep=0.5,fontproperties={'weight': 'bold','size':12})#0.5, 0.92:x-, y-direction position, maximum value:1; 2: velocity;#labelpos = [ 'N' | 'S' | 'E' | 'W' ] Position the label above, below,#to the right, to the left of the arrow, respectively. #labelsep: Distance in inches between the arrow and the label. Default is 0.1fig=plt.figure(2)Q = plt.quiver(X, Y, U, V, units='width')qk = plt.quiverkey(Q, 0.5, 0.94, 2, r'$2 \frac{m}{s}$', labelpos='E', fontproperties={'weight': 'bold'})plt.axis([-1, 7, -1, 7])fig=plt.figure(3)Q = plt.quiver(X[::3, ::3], Y[::3, ::3], U[::3, ::3], V[::3, ::3], pivot='mid', color='r')qk = plt.quiverkey(Q, 0.5, 0.03, 1, r'$1 \frac{m}{s}$', fontproperties={'weight': 'bold'})plt.plot(X[::3, ::3], Y[::3, ::3], 'k.')plt.axis([-1, 7, -1, 7])#pivot: [ 'tail' | 'mid' | 'middle' | 'tip' ]#The part of the arrow that is at the grid point; the arrow rotates#about this point, hence the name *pivot*.fig=plt.figure(4)M = np.hypot(U, V)Q = plt.quiver(X, Y, U, V, M, units='x', pivot='tip', width=0.022, scale=1 / 0.15)qk = plt.quiverkey(Q, 0.9, 1.05, 1, r'$1 \frac{m}{s}$', labelpos='E', fontproperties={'weight': 'bold'})plt.plot(X, Y, 'k.')plt.axis([-1, 7, -1, 7])#hypot(U,V) =sqrt(U**2+V**2)#M:An optional array used to map colors to the arrowsfig=plt.figure(5)Q = plt.quiver(X[::3, ::3], Y[::3, ::3], U[::3, ::3], V[::3, ::3], color='r', units='x', linewidths=2, edgecolors=('k'), headaxislength=5)qk = plt.quiverkey(Q, 0.5, 0.03, 1, r'$1 \frac{m}{s}$', fontproperties={'weight': 'bold'})plt.axis([-1, 7, -1, 7])plt.show()4. streamplot plotsExamples of streamplot plotsimport numpy as npimport matplotlib.pyplot as plt#Y, X = np.mgrid[-3:3:100j, -3:3:100j]X, Y = np.meshgrid(np.arange(-3, 3.3, .3), np.arange(-3, 3.3, .3))U = -1 - X**2 + YV = 1 + X - Y**2speed = np.sqrt(U*U + V*V)fig=plt.figure(1)strm = plt.streamplot(X, Y, U, V, color=speed, linewidth=2, cmap=plt.cm.jet)plt.colorbar(strm.lines)fig=plt.figure(2)plt.streamplot(X, Y, U, V, density=1)fig=plt.figure(3)lw = 5*speed / speed.max() # speed.max() equivalent to np.max(speed)plt.streamplot(X, Y, U, V, density=0.6, color='k', linewidth=lw)plt.show()5. imread plotsExamples of imread plotsimport matplotlib.pyplot as pltimport matplotlib.image as mpimgimport numpy as npimg=mpimg.imread(r'D:\Users\zhup\wrk\course\course10\python\matthurricane.jpg')imgplot = plt.imshow(img)plt.show() ................
................

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

Google Online Preview   Download