Python 如何创建matplotlib幻灯片?

Python 如何创建matplotlib幻灯片?,python,matplotlib,cross-platform,slideshow,Python,Matplotlib,Cross Platform,Slideshow,下面的Python/pyplot代码生成四个图形和四个窗口。我需要打开一个显示图1的窗口的代码。然后,当用户按下向右箭头按钮或向右箭头键时,同一窗口将清除图1并显示图2。因此,用户基本上只会选择四个图形中的一个进行幻灯片放映。我已经在文档和在线搜索了答案,但没有成功。我对问题进行了编辑,以显示四个图中出现的六个轴的定义。似乎必须将轴与单个图形相关联,然后绘制、清除和重画轴,以在默认GUI中模拟幻灯片 import numpy as np import matplotlib.pyplot as p

下面的Python/pyplot代码生成四个图形和四个窗口。我需要打开一个显示图1的窗口的代码。然后,当用户按下向右箭头按钮或向右箭头键时,同一窗口将清除图1并显示图2。因此,用户基本上只会选择四个图形中的一个进行幻灯片放映。我已经在文档和在线搜索了答案,但没有成功。我对问题进行了编辑,以显示四个图中出现的六个轴的定义。似乎必须将轴与单个图形相关联,然后绘制、清除和重画轴,以在默认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上获得一个非常基本的幻灯片,并在以后需要时为其他操作系统开发,我会感到满意。

可能是这样的: (单击要切换的图形)


这很好地解决了我的问题!我接受了这个答案b/c它在Windows7默认GUI中工作,支持全屏显示和调整大小。即使我在每个def语句中使用fig.set_tight_layout(True),它也能工作。但是,如果我在100 dpi下定义一个10 x 6.5英寸的图形尺寸,那么在全尺寸下重新绘制图形时会出现问题。我将为打印支持编写单独的代码。
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()