Updating a matplotlib bar graph?

前端 未结 2 1141
醉话见心
醉话见心 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:52

    I've simplified the above excellent solution to its essentials, with more details at my blogpost:

    import numpy as np
    import matplotlib.pyplot as plt
    
    numBins = 100
    numEvents = 100000
    
    file = 'datafile_100bins_100000events.histogram'
    histogramSeries = np.loadtext(file)
    
    fig, ax = plt.subplots()
    rects = ax.bar(range(numBins), np.ones(numBins)*40)  # 40 is upper bound of y-axis 
    
    for i in range(numEvents):
        for rect,h in zip(rects,histogramSeries[i,:]):
            rect.set_height(h)
        fig.canvas.draw()
        plt.pause(0.001)
    

提交回复
热议问题