1
我是slowlylearning如何用matplotlib
動畫數字。現在,我有一個條形圖,我想在每個新框架中添加一個新框(並調整其他框的寬度和高度)。在matplotlib動畫的每次迭代中添加新的箱子
這是我到目前爲止所做的。
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animation
fig = plt.figure()
ax = plt.subplot(1,1,1)
N = 10
plt.xlim(0,10)
plt.ylim(0,10)
x = np.arange(N)
y = np.zeros(N)
bars = plt.bar(x,y,1)
for bar in bars:
ax.add_patch(bar)
def init():
for bar in bars:
bar.set_height(0.)
return [bar for bar in bars]
# animation function. This is called sequentially
def animate(i):
for j, bar in enumerate(bars):
bar.set_height(j+i)
bar.set_width(bar.get_width()/float(i+1))
return [bar for bar in bars]
anim = animation.FuncAnimation(fig, animate, init_func=init,
frames = 10, interval=200, blit=True)
plt.show()
所以,在上面的代碼中,animate
應添加每i
一個新的條[1; 10],從10巴,然後11,...,以及最後20
問題:我該怎麼辦?
感謝