Python - 轴和plt.figure()
有人能看看这段代码吗?我需要做三个正弦轴的示例。
import matplotlib.pyplot as plt
import numpy as np
t = np.arange(0.0, 2.0, 0.01)
fig = plt.figure()
ax1 = fig.add_subplot(311)
y1 = np.sin(2*np.pi*t)
ax1.plot(t, y1);
ax1.grid(True)
ax1.set_ylabel('1 Hz');
ax2 = fig.add_subplot(312)
y2 = np.sin(4*np.pi*t)
ax2.plot(t, y2);
ax2.grid(True)
ax2.set_ylabel('4 Hz');
ax3 = fig.add_subplot(313)
y3 = np.sin(6*np.pi*t)
ax3.plot(t, y3);
ax3.grid(True)
ax3.set_ylabel('6 Hz');
plt.show()
有人能告诉我为什么这段代码没有任何效果吗?我只看到“图 1”,就没别的了。
1 个回答
0
试着在 plt.show()
之前加上 fig.canvas.draw()
。
一般来说,fig.canvas.draw()
是负责实际绘图的,而 plt.show()
则是进入一个可以与图形互动的状态。当你使用 IPython 或其他交互式解释器时,ion()
和 ioff()
可以在每次命令后切换更新状态。
有趣的是,你的代码在我的电脑上(Debian 不稳定版)也能正常运行——这可能是因为在新版本的 Matplotlib 中,这种行为发生了变化。