python matplotlib 图像可视化
python-data-visualization-course
Interactive Web Plotting for Python
Interactive Web Plotting for Python-github
待整理的
Matplotlib
Introduction to Matplotlib and basic line
matplotlib——一个 2D 绘图库,可产生出版物质量的图表 http://matplotlib.org/
matplotlib库则能够快速地绘制精美的图表、以多种格式输出,并且带有简单的3D绘图的功能。
matplotlib官方网址: http://matplotlib.sourceforge.net
Introduction to Matplotlib and basic line
figures and axes
import matplotlib.pyplot as plt
fig = 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 title
fig.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 limits
ax.set_ylim(3,4) #set y limimits
ax.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 plt
plt.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=5
for ks in range(1,10):ks = ks*0.1plt.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-10
for 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 np
from mayavi import mlab
x, y = np.mgrid[-3:3:150j,-3:3:150j]
z =x**2+y**2+2
surf = mlab.surf(x, y, z, colormap='RdYlBu', warp_scale='auto')
surf.actor.property.interpolation = 'phong'
#对应参数表面反射率和n高光系数
surf.actor.property.specular = 0.5 #ks
surf.actor.property.specular_power = 2 #n
mlab.show()
References
Python科学计算和绘图入门
机器学习入门必备的13张小抄