Updating a matplotlib bar graph?

前端 未结 2 1144
醉话见心
醉话见心 2020-12-11 05:41

I have a bar graph which retrieves its y values from a dict. Instead of showing several graphs with all the different values and me having to close every single one, I need

2条回答
  •  心在旅途
    2020-12-11 05:49

    Here is an example of how you can animate a bar plot. You call plt.bar only once, save the return value rects, and then call rect.set_height to modify the bar plot. Calling fig.canvas.draw() updates the figure.

    import matplotlib
    matplotlib.use('TKAgg')
    import matplotlib.pyplot as plt
    import numpy as np
    
    def animated_barplot():
        # http://www.scipy.org/Cookbook/Matplotlib/Animations
        mu, sigma = 100, 15
        N = 4
        x = mu + sigma*np.random.randn(N)
        rects = plt.bar(range(N), x,  align = 'center')
        for i in range(50):
            x = mu + sigma*np.random.randn(N)
            for rect, h in zip(rects, x):
                rect.set_height(h)
            fig.canvas.draw()
    
    fig = plt.figure()
    win = fig.canvas.manager.window
    win.after(100, animated_barplot)
    plt.show()
    

提交回复
热议问题