Can matplotlib be used to create a live-updating bar graph animation that both increments and decrements? [duplicate]

时间秒杀一切 提交于 2019-12-20 06:20:57

问题


I'm working on a project that is taking in inputs of data that represent accuracy predictions. The pseudo-idea of it is that the program will create 11 bar graphs, that are updated live (with values that increase and decrease), and it needs to be exported as a .mp4 file. The expect input is 11 lists that contain the values to be updated. My problem is that I don't really know where to begin because I can't find anything with animate that allows both incrementing and decrementing multiple bar graphs.

I've searched around for possible methods, trying to gather information. Things that seem like they work but don't are continuously redrawing the graph, but it's just throwing a new graph on the screen, it's not really live updating unless I can capture it. The code below I found originally here before updating: Updating a matplotlib bar graph? ,it hasn't been changed but this is the closest I can think of and it's using the right methodology, but it's not a true animation, and it's not being saved.

import matplotlib
matplotlib.use('TKAgg')
import matplotlib.pyplot as plt
import numpy as np
import time

def animated_barplot():
    # http://www.scipy.org/Cookbook/Matplotlib/Animations
    #mu, sigma = 1, 15
    N = 11
    x = np.random.rand(11)
    print(x)
    rects = plt.bar(range(N), x,  align = 'center')
    for i in range(50):
        x = np.random.rand(11)
        for rect, h in zip(rects, x):
            rect.set_height(h)
        fig.canvas.draw()
        time.sleep(512/22050)

fig = plt.figure()
win = fig.canvas.manager.window
win.after(100, animated_barplot)

plt.show()

Like I said at the top, the ideal goal of the code is to be able to live update multiple bar graphs that both increase and decrease in value, and to have the entire animation be saved to a video format. I don't expect someone to provide me with a full code for this, but if someone can simply point me to something useful, that would be great. Or provide me a type of pseudo-code for a possible working method for this, that would also be extremely helpful.

来源:https://stackoverflow.com/questions/55838095/can-matplotlib-be-used-to-create-a-live-updating-bar-graph-animation-that-both-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!