python - 在 matplotlib 动画模块中管理动态绘图

标签 python animation matplotlib

我想要一个迭代绘制的图表,允许跳到下一帧,停止它并返回到前一帧。

我看过 matplotlib Animation 模块,如果有一种方法可以实现前一帧功能(例如按下键时向后运行动画几帧),那将是完美的

这样的事情会很好:

def update_frame(i, data):
    fig.set_data(data[i])

但是以某种方式,我可以明确地管理 i 迭代器是增加还是减少。

有没有办法在 matplotlib 中做到这一点?
我应该寻找不同的python模块吗?

最佳答案

FuncAnimation类允许 supply a generator functionframes争论。该函数预计会产生一个值,该值将提供给动画的每个步骤的更新函数。

FuncAnimation doc状态:

frames : iterable, int, generator function, or None, optional [..]
If a generator function, then must have the signature
def gen_function() -> obj:
In all of these cases, the values in frames is simply passed through to the user-supplied func and thus can be of any type.



我们现在可以创建一个生成器函数,它产生正向或反向整数,以便动画向前运行 enter image description here或向后 enter image description here .为了控制动画,我们可以使用 matplotlib.widgets.Button s 并且还创造了一个进步enter image description here或向后 enter image description here功能。这类似于 my answer关于循环一组图像的问题。

下面是一个名为 Player 的类哪些子类FuncAnimation并结合所有这些,允许启动和停止动画。它可以像 FuncAnimation 一样实例化。 ,
ani = Player(fig, update, mini=0, maxi=10)

在哪里 update将是一个更新函数,期望一个整数作为输入,minimaxi表示函数可以使用的最小和最大数。此类存储当前索引的值 (self.i),这样如果动画停止或恢复,它将在当前帧重新开始。
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import mpl_toolkits.axes_grid1
import matplotlib.widgets

class Player(FuncAnimation):
    def __init__(self, fig, func, frames=None, init_func=None, fargs=None,
                 save_count=None, mini=0, maxi=100, pos=(0.125, 0.92), **kwargs):
        self.i = 0
        self.min=mini
        self.max=maxi
        self.runs = True
        self.forwards = True
        self.fig = fig
        self.func = func
        self.setup(pos)
        FuncAnimation.__init__(self,self.fig, self.func, frames=self.play(), 
                                           init_func=init_func, fargs=fargs,
                                           save_count=save_count, **kwargs )    

    def play(self):
        while self.runs:
            self.i = self.i+self.forwards-(not self.forwards)
            if self.i > self.min and self.i < self.max:
                yield self.i
            else:
                self.stop()
                yield self.i

    def start(self):
        self.runs=True
        self.event_source.start()

    def stop(self, event=None):
        self.runs = False
        self.event_source.stop()

    def forward(self, event=None):
        self.forwards = True
        self.start()
    def backward(self, event=None):
        self.forwards = False
        self.start()
    def oneforward(self, event=None):
        self.forwards = True
        self.onestep()
    def onebackward(self, event=None):
        self.forwards = False
        self.onestep()

    def onestep(self):
        if self.i > self.min and self.i < self.max:
            self.i = self.i+self.forwards-(not self.forwards)
        elif self.i == self.min and self.forwards:
            self.i+=1
        elif self.i == self.max and not self.forwards:
            self.i-=1
        self.func(self.i)
        self.fig.canvas.draw_idle()

    def setup(self, pos):
        playerax = self.fig.add_axes([pos[0],pos[1], 0.22, 0.04])
        divider = mpl_toolkits.axes_grid1.make_axes_locatable(playerax)
        bax = divider.append_axes("right", size="80%", pad=0.05)
        sax = divider.append_axes("right", size="80%", pad=0.05)
        fax = divider.append_axes("right", size="80%", pad=0.05)
        ofax = divider.append_axes("right", size="100%", pad=0.05)
        self.button_oneback = matplotlib.widgets.Button(playerax, label=ur'$\u29CF$')
        self.button_back = matplotlib.widgets.Button(bax, label=u'$\u25C0$')
        self.button_stop = matplotlib.widgets.Button(sax, label=u'$\u25A0$')
        self.button_forward = matplotlib.widgets.Button(fax, label=u'$\u25B6$')
        self.button_oneforward = matplotlib.widgets.Button(ofax, label=u'$\u29D0$')
        self.button_oneback.on_clicked(self.onebackward)
        self.button_back.on_clicked(self.backward)
        self.button_stop.on_clicked(self.stop)
        self.button_forward.on_clicked(self.forward)
        self.button_oneforward.on_clicked(self.oneforward)

### using this class is as easy as using FuncAnimation:            

fig, ax = plt.subplots()
x = np.linspace(0,6*np.pi, num=100)
y = np.sin(x)

ax.plot(x,y)
point, = ax.plot([],[], marker="o", color="crimson", ms=15)

def update(i):
    point.set_data(x[i],y[i])

ani = Player(fig, update, maxi=len(y)-1)

plt.show()

enter image description here

注意:这并没有以允许 blitting 的方式编写。

关于python - 在 matplotlib 动画模块中管理动态绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44985966/

相关文章:

python - 如何使用环境模块文件(tcl 脚本)加载 virtualenv?

将 secret 消息翻译成真实字母的Python程序

javascript - 重新渲染组件 React

python - 垫秀 : colormap the values in numpy array to binary

python - 在 Python 2.6 中导入 Pylab 时出现问题

Python tnsnames.ora 解析器

python - 如何删除 pytest-html 报告中的环境表

javascript - 我如何在第一次迭代时反转 CSS3 动画(而不是第二次迭代)

css - 动画过渡没有从正确的位置开始

python - mplfinance 是否有对数刻度设置?