【问题标题】:How to add a time control panel to a FuncAnimation from matplotlib如何从 matplotlib 向 FuncAnimation 添加时间控制面板
【发布时间】:2015-06-07 02:18:19
【问题描述】:

我目前正在使用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)

【问题讨论】:

    标签: python-2.7 animation matplotlib


    【解决方案1】:

    您所说的是一个 GUI。最简单的例子使用 matplotlib 内置的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 示例。

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

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

    【讨论】:

    • 这太棒了!我很高兴能尝试让它适应我的工作。非常感谢!
    • 你给我的东西很棒。但我太热情了,现在很难让它适应我的具体情况。我不知道如何用我的数据替换您选择作为示例显示给我的数据。我有一个具有形状的文件:256,512,512。 256是时间点,512都是X和Y的。我认为我所有的麻烦都来自 Z 的含义以及如何像您的示例中那样使用它
    • GUI 编程刚开始时有点奇怪。我添加了一个版本,它在大小为 256,512,512 的数组中移动结果。需要注意的主要是stime 被实例化为一个滑块对象,stime.on_changed(update) 绑定了例程update,所以每次滑块更改值时都会调用此例程,stime.val 给出滑块上的当前值。我以为你想要一个彩色网格。如果要绘制线数据,可以为 y 位置添加一个滑块,该滑块会更改数组的索引。
    • 非常感谢您的帮助!所以我尝试了你给我的东西。我在之前的工作结束时添加了代码。所以我想在第三个图中尝试你的东西(图 1 是我的动画,图 2 是我使用的一些情节)。那么我得到什么?我很好地让 GUI 与动画一起工作,可以根据需要进行时间转换,但是我的主动画中的数据现在很奇怪,不是完全错误但仍然很奇怪,所以我不能说它正在工作啊哈
    • 我删除了脚本的第一部分。所以现在我有了我的数据,以及你给我的数据。我有同样奇怪的结果。第一张图片,即 50 的那张(在脚本中定义)与其他图片看起来完全不同,当我回到 50% 时,我与打开时没有相同的东西,这是否正常? ^^'
    猜你喜欢
    • 2014-08-06
    • 2021-12-20
    • 1970-01-01
    • 1970-01-01
    • 2012-06-09
    • 1970-01-01
    • 2013-09-15
    • 1970-01-01
    相关资源
    最近更新 更多