Updating the matplotlib histogram?

I have a histogram that extracts its y values ​​from a dict. Instead of showing multiple plots with all the different values, and I need to close each of them, I need it to update the values ​​on the same plot. Is there a solution for this?

+4
source share
2 answers

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

 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() 
+9
source

I have simplified the above excellent solution inherently, with more details on the blogpost :

 import numpy as np import matplotlib.pyplot as plt numBins = 100 numEvents = 100000 file = 'datafile_100bins_100000events.histogram' histogramSeries = np.fromfile(file, int).reshape(-1,numBins) 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): [rect.set_height(h) for rect,h in zip(rects,histogramSeries[i,:])] fig.canvas.draw() plt.pause(0.001) 
0
source

Source: https://habr.com/ru/post/1398899/


All Articles