注意
转到末尾以下载完整的示例代码。
在pyplot中管理多个图像#
matplotlib.pyplot
使用“当前图像”(current figure)和“当前坐标轴”(current Axes)的概念。图像通过传递给 figure
的图像编号来标识。具有给定编号的图像被设置为“当前图像”。此外,如果不存在具有该编号的图像,则会创建一个新的图像。
注意
我们不建议通过隐式的pyplot接口处理多个图像,因为管理“当前图像”(current figure)既繁琐又容易出错。相反,我们推荐使用显式方法,直接在Figure和Axes实例上调用方法。有关隐式和显式接口之间权衡的解释,请参阅Matplotlib 应用程序接口 (APIs)。
创建图像 1
plt.figure(1)
plt.subplot(211)
plt.plot(t, s1)
plt.subplot(212)
plt.plot(t, 2*s1)

创建图像 2
plt.figure(2)
plt.plot(t, s2)

现在切换回图像 1 并进行一些更改
plt.figure(1)
plt.subplot(211)
plt.plot(t, s2, 's')
ax = plt.gca()
ax.set_xticklabels([])
plt.show()

脚本总运行时间: (0 minutes 2.082 seconds)