Python 中的绘图matplotlib & mayavi库

来源:互联网 发布:nginx 全局变量 编辑:程序博客网 时间:2024/05/22 05:33

这里写图片描述


待整理的

Matplotlib
Introduction to Matplotlib and basic line


matplotlib库则能够快速地绘制精美的图表、以多种格式输出,并且带有简单的3D绘图的功能。

matplotlib官方网址: http://matplotlib.sourceforge.net

Introduction to Matplotlib and basic line


figures and axes


import matplotlib.pyplot as pltfig = plt.figure(figsize=(5, 2), facecolor='black')ax = fig.add_subplot(3, 2, 2)fig, axes = plt.subplots(5, 2, figsize=(5, 5))ax = fig.add_axes([left, bottom, width, height])

figures and axes properities


fig.suptitle('title')   # big figure titlefig.subplots_adjust(bottom = 0.1, right=0.8, top=0.9, wspace=0.2, hspce=0.5)fig.tight_layout(pad=0.1, h_pad=0.5, w_pad=0.5, rect=None)ax.set_xlabel('xbla')ax.set_ylabel('ybla')ax.set_xlim(1, 2)  #sets x limitsax.set_ylim(3,4)   #set y limimitsax.set_title('blabla')ax.set(xlable='bla')ax.legend(loc='upper center')ax.grid(True, which='both')bbox = ax.get_position()bbox.xo + bbox.width

plotting routines


ax.plot(x,y, '-o', c='red', lw=2, lable='bla')ax.scatter(x,y, s=20, c=color)ax.pcolormesh(xx, yy, zz, shading ='gouraud')ax.colormesh(xx, yy, zz, norm=norm)ax.contour(xx, yy, zz, cmap='jet')ax.contourf(xx, yy, zz, vmin=2, vmax=4)n, bins, patch = ax,hist(x, 50)ax.imshow(matrix, origin ='lower', extent(x1, x2, y1, y2))ax.specgram(y, FS=0.1, noverlap=128, scale ='linear')

2D绘图


#序之前的代码def functionI(fi,ks,n,a,e0):    return e0*ks*((-350*np.sin(fi)+(700*np.sqrt(3)/2)*np.cos(fi))**n)*a*a*np.cos(fi)from matplotlib import pyplot as pltplt.figure(1)#图1:n不变,ks变化#fi的取值范围 0-pi/2,分100个点fi = np.linspace(0,np.pi/2,100)ax1 = plt.subplot(211)plt.sca(ax1)#ks = 0.1-0.9 n=5for ks in range(1,10):    ks = ks*0.1    plt.plot(fi,functionI(fi,ks,3,1,e0),label='ks='+str(ks))    plt.legend(loc='upper right',bbox_to_anchor = (1, 1.15))plt.xlabel(u'角度fi')plt.ylabel(u'n=5,ks变化   辐射强度I')ax2 = plt.subplot(212)#ks = 0.5,n=2-10for n in range(2,11):    plt.plot(fi,functionI(fi,0.5,n,1,e0),label='n='+str(n))    plt.legend(loc='upper right',bbox_to_anchor = (1, 1.15))plt.xlabel(u'角度fi')plt.ylabel(u'ks=0.5,n变化   辐射强度I')plt.show()

np.linspace:构造线性list,用来取点,其实画函数图像的本质就是画很多点,然后连接起来。
plt.figure:运行以后,一个figure对应一个plt.show(),其实就是对应一个窗口。
plt.subplot:一个figure里有多少个图像(坐标轴),每次运行这个返回一个ax坐标轴对象,每次走完这行代码,就选中这个ax,接下来的操作都是在这个ax上完成的。
plt.legend:图例显示。
以上函数的所有的参数都可以在matplotlib参考文档中找到。


3D建模


import numpy as npfrom mayavi import mlabx, y = np.mgrid[-3:3:150j,-3:3:150j]z =x**2+y**2+2surf = mlab.surf(x, y, z, colormap='RdYlBu', warp_scale='auto')surf.actor.property.interpolation = 'phong'#对应参数表面反射率和n高光系数surf.actor.property.specular = 0.5 #kssurf.actor.property.specular_power = 2 #nmlab.show()

References


Python科学计算和绘图入门

机器学习入门必备的13张小抄

0 0
原创粉丝点击