-
Figure的组成
-
两种绘图接口
-
显式创建figure和axes,在上面调用绘图方法,也被称为OO模式(object-oriented style)
-
依赖pyplot自动创建figure和axes,并绘图
-
第一种:OO模式
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()
第二种:依赖pyplot
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()
综上,fig,ax = plt.subplots()等价于:
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
- 创建子图的方式对比
fig,ax = plt.subplots(figsize=(5,5))
ax1 = fig.add_subplot(221)
ax2 = fig.add_subplot(222)
ax3 = fig.add_subplot(223)
ax4 = fig.add_subplot(224)
ax = plt.figure(figsize=(5,5))
ax1 = ax.add_subplot(221)
ax2 = ax.add_subplot(222)
ax3 = ax.add_subplot(223)
ax4 = ax.add_subplot(224)