Animated interactive plot using matplotlib

后端 未结 2 1859
栀梦
栀梦 2021-01-05 19:21

While looking for a way to make animated interactive plot using matplotlib, I encountered this piece of code on Stack overflow documentation:

import numpy as         


        
2条回答
  •  孤街浪徒
    2021-01-05 20:27

    Here is a simple adaptation of your code to add animation:

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

    The main change is the addition of the update_plot function, which is used to make a FuncAnimation in the second to last line. The animation increments from the last slider value that was set.

    The variable is_manual keeps track of when the user has clicked on the slider. After the user clicks on it, the variable is set to True and the animation will no longer update the plot.

    To resume animation, I added an on_click function which sets is_manual = False when the user clicks somewhere on the canvas OTHER than the slider.

    Since this is a quick-and-dirty script I left variables as global, but you could easily write it up in a proper class.

    Note that calling samp.set_val implicitly calls the update_slider function, which is also called when the user clicks directly on the slider, so we have to reset is_manual in the update_plot function.

提交回复
热议问题