如何在Python中为正在更新的条形字符设置动画
我想创建一个动画的堆叠条形图 有一个演示如何设置线图动画的 但是,对于条形图动画,BarContainer对象没有任何“set_data”属性。因此,每次我都必须清除图形轴,例如如何在Python中为正在更新的条形字符设置动画,python,animation,matplotlib,Python,Animation,Matplotlib,我想创建一个动画的堆叠条形图 有一个演示如何设置线图动画的 但是,对于条形图动画,BarContainer对象没有任何“set_data”属性。因此,每次我都必须清除图形轴,例如 fig=plt.figure() def init(): plt.cla() def animate(i): p1 = plt.bar(x_points,y_heights,width,color='b') return p1 anim = animation.FuncAnimation(f
fig=plt.figure()
def init():
plt.cla()
def animate(i):
p1 = plt.bar(x_points,y_heights,width,color='b')
return p1
anim = animation.FuncAnimation(fig,animate,init_func=init,frames=400,interval=10,blit=False)
是否有其他选项,遵循链接的样式,这样我就不必每次都清除轴?谢谢。
plt.bar
返回矩形列表。列表的长度等于条数。如果要更改第一个条的高度,可以执行p1[0]。设置高度(新高度)
和类似的宽度
以及一系列其他矩形属性
如前所述,如果在x中设置,则在dir(p1[0])中设置x的输出将为您提供所有可以设置的潜在属性。您需要在
动画()之外调用plt.bar
,使用矩形更新每个条的高度。当新数据进入时设置高度
实际上,使用plt.bar()返回的矩形列表压缩每个传入的y_高度集,如下所示
p1 = plt.bar(x_points,y_heights,width,color='b')
def animate(i):
for rect, y in zip(p1, y_heights):
rect.set_height(y)
anim = animation.FuncAnimation(fig,animate,init_func=init,frames=400,interval=10,blit=False)
您可能希望将p1
放入init()
中,但这取决于您自己
这个答案的所有功劳都归于联合国大学在相关问题中的答案。我本想在评论中加上一句,但我显然是个新手