How to update a plot in matplotlib?

后端 未结 7 1770
不思量自难忘°
不思量自难忘° 2020-11-22 02:11

I\'m having issues with redrawing the figure here. I allow the user to specify the units in the time scale (x-axis) and then I recalculate and call this function plots

7条回答
  •  灰色年华
    2020-11-22 02:53

    This worked for me. Repeatedly calls a function updating the graph every time.

    import matplotlib.pyplot as plt
    import matplotlib.animation as anim
    
    def plot_cont(fun, xmax):
        y = []
        fig = plt.figure()
        ax = fig.add_subplot(1,1,1)
    
        def update(i):
            yi = fun()
            y.append(yi)
            x = range(len(y))
            ax.clear()
            ax.plot(x, y)
            print i, ': ', yi
    
        a = anim.FuncAnimation(fig, update, frames=xmax, repeat=False)
        plt.show()
    

    "fun" is a function that returns an integer. FuncAnimation will repeatedly call "update", it will do that "xmax" times.

提交回复
热议问题