最佳答案
以下是如何为条形图设置动画的示例.
您只调用plt.bar一次,保存返回值rects,然后调用rect.set_height来修改条形图.
调用fig.canvas.draw()更新图形.
原文链接:https://www.f2er.com/python/439709.html您只调用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()