matplotlib提供了两种最常用的绘图接口

  1. 显式创建figure和axes,在上面调用绘图方法,也被称为OO模式(object-oriented style)

  2. 依赖pyplot自动创建figure和axes,并绘图

使用第一种绘图接口,是这样的:

x = np.linspace(0, 2, 100)

fig, ax = plt.subplots()  
ax.plot(x, x, label='linear')  
ax.plot(x, x**2, label='quadratic')  
ax.plot(x, x**3, label='cubic')  
ax.set_xlabel('x label') 
ax.set_ylabel('y label') 
ax.set_title("Simple Plot")  
ax.legend() 
plt.show()

而如果采用第二种绘图接口,绘制同样的图,代码是这样的:

x = np.linspace(0, 2, 100)

plt.plot(x, x, label='linear') 
plt.plot(x, x**2, label='quadratic')  
plt.plot(x, x**3, label='cubic')
plt.xlabel('x label')
plt.ylabel('y label')
plt.title("Simple Plot")
plt.legend()
plt.show()
plt.savefig("barChart.jpg")