Matplotlib动画:从scipy数组动画圆圈位置

时间:2014-05-09 10:45:52

标签: python animation matplotlib

我正在尝试在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()

抛出以下错误:

  File "C:\Python27\Lib\site-packages\matplotlib\transforms.py", line 2242, in inverted
    return CompositeGenericTransform(self._b.inverted(), self._a.inverted()    )
  File "C:\Python27\Lib\site-packages\matplotlib\transforms.py", line 1680, in inverted
    self._inverted = Affine2D(inv(mtx), shorthand_name=shorthand_name)
  File "C:\Python27\Lib\site-packages\numpy\linalg\linalg.py", line 520, in inv
    ainv = _umath_linalg.inv(a, signature=signature, extobj=extobj)
  File "C:\Python27\Lib\site-packages\numpy\linalg\linalg.py", line 90, in _raise_linalgerror_singular
    raise LinAlgError("Singular matrix")
LinAlgError: Singular matrix

1 个答案:

答案 0 :(得分:0)

我在你的代码中修了几个部分。

  1. 我在data中添加了一些值,以便圈子有效。
  2. 您可能希望在每个时间步骤中将圈子的中心放在data中,然后for中的animate循环不是必需的。
  3. 如果您不使用animation后端,patches功能似乎无法与Mac上的Qt4Agg一起使用。如果你使用Mac,你可能需要在下面添加前两行。

  4. import matplotlib
    matplotlib.use('Qt4Agg')
    
    import numpy as np
    from matplotlib import pyplot
    from matplotlib import animation
    from math import sin
    
    data = np.zeros((1000,4))
    
    data[:,0] = [20*(1+sin(float(x)/200)) for x in range(1000)]
    data[:,1] = [20*(1+sin(float(x)/100)) for x in range(1000)]
    data[:,2] = [20*(1+sin(float(x)/50)) for x in range(1000)]
    data[:,3] = [20*(1+sin(float(x)/25)) for x in range(1000)]
    
    fig=pyplot.figure()
    ax = pyplot.axes(xlim=(0, 40), ylim=(0, 40))
    
    circle1=pyplot.Circle((data[0,0],1.0),0.2,fc='y')
    circle2=pyplot.Circle((data[0,1],1.0),0.2,fc='g')
    circle3=pyplot.Circle((data[0,2],1.0),0.2,fc='r')
    circle4=pyplot.Circle((data[0,3],1.0),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=(data[i,0],1)
        circle2.center=(data[i,1],1)
        circle3.center=(data[i,2],1)
        circle4.center=(data[i,3],1)
        return circle1, circle2, circle3, circle4
    
    
    anim=animation.FuncAnimation(fig,animate,init_func=init,frames=1000,blit=True)
    
    pyplot.show()