[英]Updating a matplotlib bar graph?
我有一個條形圖,它從字典中檢索其 y 值。 我不需要顯示具有所有不同值的多個圖形,而且我必須關閉每個圖形,而是需要它來更新同一圖形上的值。 有解決方案嗎?
以下是如何為條形圖設置動畫的示例。 您只調用plt.bar
一次,保存返回值rects
,然后調用rect.set_height
來修改條形圖。 調用fig.canvas.draw()
更新圖形。
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()
我已經將上述出色的解決方案簡化為它的基本要素,並在我的博客文章中提供了更多詳細信息:
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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.