【问题标题】:Animated interactive plot using matplotlib使用 matplotlib 的动画交互式绘图
【发布时间】:2017-09-20 14:56:58
【问题描述】:

在寻找使用 matplotlib 制作动画交互式绘图的方法时,我在 Stack Overflow 文档中遇到了这段代码:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from matplotlib.widgets import Slider

TWOPI = 2*np.pi

fig, ax = plt.subplots()

t = np.arange(0.0, TWOPI, 0.001)
initial_amp = .5
s = initial_amp*np.sin(t)
l, = plt.plot(t, s, lw=2)

ax = plt.axis([0,TWOPI,-1,1])

axamp = plt.axes([0.25, .03, 0.50, 0.02])
# Slider
samp = Slider(axamp, 'Amp', 0, 1, valinit=initial_amp)

def update(val):
    # amp is the current value of the slider
    amp = samp.val
    # update curve
    l.set_ydata(amp*np.sin(t))
    # redraw canvas while idle
    fig.canvas.draw_idle()

# call update function on slider value change
samp.on_changed(update)

plt.show()

这段代码几乎完全符合我的要求,但我希望为情节设置动画,即让滑块自动从左向右移动,例如每秒 0.01 的进度。有什么简单的方法吗?知道我还想保留滑块上的手动控制(使用点击事件)。

【问题讨论】:

    标签: python matplotlib widget interactive


    【解决方案1】:

    您可以修改 this answer 中的代码以包含滑块。

    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.update, 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.slider.set_val(self.i)
            self.fig.canvas.draw_idle()
    
        def setup(self, pos):
            playerax = self.fig.add_axes([pos[0],pos[1], 0.64, 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)
            sliderax = divider.append_axes("right", size="500%", pad=0.07)
            self.button_oneback = matplotlib.widgets.Button(playerax, label='$\u29CF$')
            self.button_back = matplotlib.widgets.Button(bax, label='$\u25C0$')
            self.button_stop = matplotlib.widgets.Button(sax, label='$\u25A0$')
            self.button_forward = matplotlib.widgets.Button(fax, label='$\u25B6$')
            self.button_oneforward = matplotlib.widgets.Button(ofax, label='$\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)
            self.slider = matplotlib.widgets.Slider(sliderax, '', 
                                                    self.min, self.max, valinit=self.i)
            self.slider.on_changed(self.set_pos)
    
        def set_pos(self,i):
            self.i = int(self.slider.val)
            self.func(self.i)
    
        def update(self,i):
            self.slider.set_val(i)
    
    
    ### 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()
    

    【讨论】:

    • 这是一个很棒的工具,您可以考虑将其提交给 matplotlib。
    • 我不得不将字符串文字 ur'$\u29CF$' 更改为 '$\u29CF$' 以使示例在 Python 3.7 中运行而不会出现语法错误,但不确定规则是如何准确更改的。否则工作正常。
    • @BasSwinckels 没错,这个答案是为与 python 2 一起使用而编写的,它现在已经过时了。我替换了字符串。
    【解决方案2】:

    以下是添加动画的代码的简单改编:

    import numpy as np
    import matplotlib.pyplot as plt
    import matplotlib.animation as animation
    from matplotlib.widgets import Slider
    
    TWOPI = 2*np.pi
    
    fig, ax = plt.subplots()
    
    t = np.arange(0.0, TWOPI, 0.001)
    initial_amp = .5
    s = initial_amp*np.sin(t)
    l, = plt.plot(t, s, lw=2)
    
    ax = plt.axis([0,TWOPI,-1,1])
    
    axamp = plt.axes([0.25, .03, 0.50, 0.02])
    # Slider
    samp = Slider(axamp, 'Amp', 0, 1, valinit=initial_amp)
    
    # Animation controls
    is_manual = False # True if user has taken control of the animation
    interval = 100 # ms, time between animation frames
    loop_len = 5.0 # seconds per loop
    scale = interval / 1000 / loop_len
    
    def update_slider(val):
        global is_manual
        is_manual=True
        update(val)
    
    def update(val):
        # update curve
        l.set_ydata(val*np.sin(t))
        # redraw canvas while idle
        fig.canvas.draw_idle()
    
    def update_plot(num):
        global is_manual
        if is_manual:
            return l, # don't change
    
        val = (samp.val + scale) % samp.valmax
        samp.set_val(val)
        is_manual = False # the above line called update_slider, so we need to reset this
        return l,
    
    def on_click(event):
        # Check where the click happened
        (xm,ym),(xM,yM) = samp.label.clipbox.get_points()
        if xm < event.x < xM and ym < event.y < yM:
            # Event happened within the slider, ignore since it is handled in update_slider
            return
        else:
            # user clicked somewhere else on canvas = unpause
            global is_manual
            is_manual=False
    
    # call update function on slider value change
    samp.on_changed(update_slider)
    
    fig.canvas.mpl_connect('button_press_event', on_click)
    
    ani = animation.FuncAnimation(fig, update_plot, interval=interval)
    
    plt.show()
    

    主要的变化是增加了update_plot函数,用于在倒数第二行创建一个FuncAnimation。动画从设置的最后一个滑块值开始递增。

    变量is_manual 跟踪用户单击滑块的时间。用户点击后,变量设置为True,动画将不再更新剧情。

    为了恢复动画,我添加了一个on_click 函数,当用户单击画布上除滑块以外的某个位置时,它设置is_manual = False

    由于这是一个简单粗暴的脚本,我将变量保留为全局变量,但您可以轻松地将其写在适当的类中。

    注意调用samp.set_val会隐式调用update_slider函数,当用户直接点击滑块时也会调用该函数,所以我们必须在update_plot函数中重置is_manual

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-04-08
      • 2021-01-31
      • 1970-01-01
      • 2018-04-28
      • 1970-01-01
      • 2012-02-26
      • 2012-05-29
      相关资源
      最近更新 更多