常用的几种绘图
一: 折线图
通常用来反应数据的变化趋势:
import matplotlib.pyplot as plt fig = plt.figure()#创建画板 ax1 = fig.add_subplot(2,2,1)#.add_subplot添加子图 ax2 = fig.add_subplot(2,2,2) #ax3 = fig.add_subplot(2,2,3) ax4 = fig.add_subplot(2,2,4) #ax4 = fig.add_subplot(224) plt.show()
fig = plt.figure() #fig = plt.figure(figsize=(8, 15))#figsize=(8, 15)设置画板大小 ax1 = fig.add_subplot(2,1,1) ax2 = fig.add_subplot(2,1,2) ax1.plot(np.random.randint(1,5,5), np.arange(5)) #np.random.randint(low,high,size),生成在[low,high)随机整数,low默认是0,size是元素个数,size是元组时,生成矩阵 ax2.plot(np.arange(10)*3, np.arange(10)) plt.show()
二:散点图
>>> lines = ax.plot(x,y) Draw points with lines or markers connecting them >>> ax.scatter(x,y) Draw unconnected points, scaled or colored >>> axes[0,0].bar([1,2,3],[3,4,5]) Plot vertical rectangles (constant width) >>> axes[1,0].barh([0.5,1,2.5],[0,1,2]) Plot horiontal rectangles (constant height) >>> axes[1,1].axhline(0.45) Draw a horizontal line across axes >>> axes[0,1].axvline(0.65) Draw a vertical line across axes >>> ax.fill(x,y,color='blue') Draw filled polygons >>> ax.fill_between(x,y,color='yellow') Fill between y-values and 0
三:三维
from mpl_toolkits.mplot3d import Axes3D fig = plt.figure() ax = Axes3D(fig) x = np.arange(-2,2,0.1) y = np.arange(-2,2,0.1) x,y = np.meshgrid(x,y)#np.meshgrid()可用于生成网格数据对 def f(x,y): return (x**2+y**2) ax.plot_surface(x,y,f(x,y),color='r')#.plot_surface()画空间面图 ax.set_xlabel('x label') ax.set_ylabel('y label') ax.set_zlabel('y label') plt.show()