Coding styles 编码方式
在matplotlib中,有两种绘图方式:OO-style(object-oriented)、pyplot interface。
- OO-style:分别创建Figures 和Axes,并使用其对应的方法绘图
- 依靠pyplot来自动创建和管理Figures和Axes,应且使用pyplot函数来绘图
例如使用OO-style:
x=np.linspace(0,2,100)
fig, ax=plt.subplots(figsize=(5,2.7),contrained_layout=True)
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()
结果如图:
例如使用pyplot-style
x=np.linspace(0,2,100)
plt.figure(figsize=(5,2.7),constrained_layout=True) # pyplt-