如何从 matplotlib 向 FuncAnimation 添加时间控制面板

Mag*_*gea 5 animation matplotlib python-2.7

我目前正在使用matplotlib.animation.FuncAnimation()在图形上显示我的作品的动画。

它工作得很好,我理解我使用的参数(间隔、时间范围,...)但是,我想知道是否有办法实现(可能直接到图)包含动画的面板,一个滚动条或其他任何东西,它允许我:

  • 快速向前或向后移动到感兴趣的时区。
  • 显示我在动画的哪个点(10%,然后是 20%,...)。

基本上,是一种控制图形上python中的动画的方法,就像我将其控制为视频播放器播放的视频文件一样?

如果需要,这就是这个动画的代码:

    def init():
        im1.set_data(XYslice[0, :, :])
        im2.set_data(XZslice[0, Nplans/2:, :])
        return([im1, im2])

    def animate(t):
       im1.set_data(XYslice[t, :, :])
       im2.set_data(XZslice[t, Nplans/2:, :])
       return [im1, im2]

    anim = animation.FuncAnimation(fig, animate, np.arange(Ntime), interval=200,
                                 blit=True, init_func=init, repeat=True)
Run Code Online (Sandbox Code Playgroud)

Ed *_*ith 5

你说的是GUI。最简单的示例使用 matplotlib 内置小部件

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.mlab import bivariate_normal
from matplotlib.widgets import Slider, Button

#Setup figure and data
fig, ax = plt.subplots()
plt.subplots_adjust(bottom=0.25)
delta = 0.5
t = np.arange(0.0, 100.0, 0.1)
x = np.arange(-3.0, 4.001, delta)
y = np.arange(-4.0, 3.001, delta)
X, Y = np.meshgrid(x, y)
Z1 = bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
Z2 = bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
Z = (Z1 - Z2) * 5.
cmap = plt.cm.rainbow
im = ax.pcolormesh(X, Y, Z, cmap=cmap)
fig.colorbar(im)
axcolor = 'lightgoldenrodyellow'
axtime = plt.axes([0.25, 0.1, 0.65, 0.03], axisbg=axcolor)
stime = Slider(axtime, 'Time', 0.0, 100.0, valinit=50.0)

#Routines to reset and update sliding bar
def reset(event):
    stime.reset()

def update(val):
    time = stime.val/10.
    Z = (Z1 - Z2) * time
    im.set_array(Z.ravel())
    fig.canvas.draw()

#Bind sliding bar and reset button  
stime.on_changed(update)
resetax = plt.axes([0.8, 0.025, 0.1, 0.04])
button = Button(resetax, 'Reset', color=axcolor, hovercolor='0.975')
button.on_clicked(reset)

plt.show()
Run Code Online (Sandbox Code Playgroud)

这应该是一个开始。如果您希望它看起来更好(并添加更多功能),那么您需要使用类似 的 GUI 框架wxpython,请查看示例。

更符合您的数据结构的示例如下:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.mlab import bivariate_normal
from matplotlib.widgets import Slider, Button

#Setup figure and data
fig, ax = plt.subplots()
plt.subplots_adjust(bottom=0.25)
delta = 0.5
t = np.linspace(0.0, 100.0, 256)
x = np.linspace(-4.0, 4.001, 512)
y = np.linspace(-4.0, 4.001, 512)
X, Y = np.meshgrid(x, y)
Z1 = bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
Z2 = bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
XZslice = np.zeros((256,512,512))
for i in range(t.shape[0]):
    XZslice[i,:,:] = (Z1 - Z2) * t[i]/10.
cmap = plt.cm.rainbow
im = ax.pcolormesh(XZslice[128,:,:], cmap=cmap)
fig.colorbar(im)
axcolor = 'lightgoldenrodyellow'
axtime = plt.axes([0.25, 0.1, 0.65, 0.03], axisbg=axcolor)
stime = Slider(axtime, 'Time', 0.0, 100.0, valinit=50.0)

#Routines to reset and update sliding bar
def reset(event):
    stime.reset()

def update(val):
    time = int(stime.val/100.* 256)
    im.set_array(XZslice[time,:,:].ravel())
    fig.canvas.draw()

#Bind sliding bar and reset button  
stime.on_changed(update)
resetax = plt.axes([0.8, 0.025, 0.1, 0.04])
button = Button(resetax, 'Reset', color=axcolor, hovercolor='0.975')
button.on_clicked(reset)

plt.show()
Run Code Online (Sandbox Code Playgroud)