下面的Python / pyplot代码生成四个图形和四个窗口。我需要打开一个显示fig1的窗口的代码。然后,当用户按下向右箭头按钮或向右箭头键时,同一窗口将清除fig1并显示fig2。因此,基本上,用户只能选择四个图形之一来进行幻灯片显示。我在文档和在线搜索中均未找到答案。我已经编辑了问题,以显示出现在四个图中的六个轴的定义。看来必须将轴与单个图形关联起来,然后绘制,清除和重绘轴才能在默认GUI中模拟幻灯片显示?
import numpy as np
import matplotlib.pyplot as plt
fig1 = plt.figure()
ax1 = fig1.add_subplot(3, 1, 1)
ax2 = fig1.add_subplot(3, 1, 2, sharex=ax1)
ax3 = fig1.add_subplot(3, 1, 3, sharex=ax1)
fig2 = plt.figure()
ax4 = fig2.add_subplot(1, 1, 1)
fig3 = plt.figure()
ax5 = fig2.add_subplot(1, 1, 1)
fig4 = plt.figure()
ax6 = fig2.add_subplot(1, 1, 1)
plt.show()
理想情况下,我想设置后端以确保在MacOS,Linux和Windows上具有相同的代码功能。但是,我很高兴能在Windows 7上获得非常基本的幻灯片演示,并在必要时稍后为其他OS开发。
也许是这样的:(单击该图进行切换)
import matplotlib.pyplot as plt
import numpy as np
i = 0
def fig1(fig):
ax = fig.add_subplot(111)
ax.plot(x, np.sin(x))
def fig2(fig):
ax = fig.add_subplot(111)
ax.plot(x, np.cos(x))
def fig3(fig):
ax = fig.add_subplot(111)
ax.plot(x, np.tan(x))
def fig4(fig):
ax1 = fig.add_subplot(311)
ax1.plot(x, np.sin(x))
ax2 = fig.add_subplot(312)
ax2.plot(x, np.cos(x))
ax3 = fig.add_subplot(313)
ax3.plot(x, np.tan(x))
switch_figs = {
0: fig1,
1: fig2,
2: fig3,
3: fig4
}
def onclick1(fig):
global i
print(i)
fig.clear()
i += 1
i %= 4
switch_figs[i](fig)
plt.draw()
x = np.linspace(0, 2*np.pi, 1000)
fig = plt.figure()
switch_figs[0](fig)
fig.canvas.mpl_connect('button_press_event', lambda event: onclick1(fig))
plt.show()
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句