我正在尝试在matplotlib中设置圆的位置动画,但我不认为它是正确的
我的数据是二维矩阵大小((1000,4))每行包含4个圆的y位置,x总是1
import numpy as np
from matplotlib import pyplot
from matplotlib import animation
data = np.zeros((1000,4))
fig=pyplot.figure()
ax=pyplot.axes([0,40,0,40])
circle1=pyplot.Circle((data[0,0],1),0.2,fc='y')
circle2=pyplot.Circle((data[0,1],1),0.2,fc='g')
circle3=pyplot.Circle((data[0,2],1),0.2,fc='r')
circle4=pyplot.Circle((data[0,3],1),0.2,fc='b')
def init():
circle1.center=(data[0,0],1)
circle2.center=(data[0,1],1)
circle3.center=(data[0,2],1)
circle4.center=(data[0,3],1)
ax.add_patch(circle1)
ax.add_patch(circle2)
ax.add_patch(circle3)
ax.add_patch(circle4)
return circle1, circle2, circle3, circle4
def animate(i):
for state in data:
circle1.center=(state[0],1)
circle2.center=(state[1],1)
circle3.center=(state[2],1)
circle4.center=(state[3],1)
return circle1, circle2, circle3, circle4
anim=animation.FuncAnimation(fig,animate,init_func=init,frames=1000,blit=True)
pyplot.show()
将引发以下错误:
^{pr2}$
我修复了你代码中的一些部分。在
data
中添加了一些值,以便使圆具有动画效果。在data
中,那么animate
中的for
循环是不必要的。在Qt4Agg
后端,在Mac上animation
函数似乎不能与{相关问题 更多 >
编程相关推荐