如何从matplotlib将时间控制面板添加到FuncAnimation [英] How to add a time control panel to a FuncAnimation from matplotlib

查看:266
本文介绍了如何从matplotlib将时间控制面板添加到FuncAnimation的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前正在使用 matplotlib.animation.FuncAnimation () 来在数字上显示我的作品的动画。

I am currently using matplotlib.animation.FuncAnimation() to display an animation of my work, on a figure.

效果很好,我理解我正在使用的参数(interval,time range等),但是,我想知道是否有一种方法可以实现(可能直接针对该图)包含动画,滚动条或其他内容的面板,这使我能够:

It is working very well, and I understand the arguments I am using ( interval, time range , ...) However, I was wondering if there was a way to implement (maybe directly to the figure) a panel containing the animation, a scroll-bar or whatever, which allows me to :


  • 快速前进或后退到感兴趣的时区。

  • 显示在什么时间点我是动画的一部分(10%,然后是20%,...)。

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

Basically, is a way to control the animation in python on the figure like the way I would control it as a video file played by a video player?

如果需要的话,此动画的代码如下所示:

If needed, this is what looks like the code for this animation :

    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)


推荐答案

您所说的是一个GUI。最简单的示例使用内置的matplotlib widgets

What you are talking about is a GUI. The simplest example uses the matplotlib inbuilt widgets:

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()

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

This should be a start. If you want it to look better (and add more functionality) then you need to go to a GUI framework like wxpython, check out this example.

与您的数据结构更内联的示例如下:

An example which is more inline with your data-structure would go as follows:

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()

这篇关于如何从matplotlib将时间控制面板添加到FuncAnimation的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆