Python 2.7 Python matplotlib动画圆弧

Python 2.7 Python matplotlib动画圆弧,python-2.7,matplotlib,Python 2.7,Matplotlib,我正在尝试设置圆弧和圆的动画。圆圈每帧都在移动。当圆弧根据圆的位置改变半径、位置和消失时 我正在尝试设置这些圆弧的动画,但它们没有改变 下面是代码示例: import matplotlib import matplotlib.pyplot as plt import matplotlib.animation as animation from matplotlib import patches import numpy as np particle_one = np.zeros((10,2))

我正在尝试设置圆弧和圆的动画。圆圈每帧都在移动。当圆弧根据圆的位置改变半径、位置和消失时

我正在尝试设置这些圆弧的动画,但它们没有改变

下面是代码示例:

import matplotlib
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from matplotlib import patches
import numpy as np

particle_one = np.zeros((10,2)) #10 times steps and x,y positions
particle_two = np.zeros((10,2)) #10 times steps and x,y positions

#the two particles are moving away from each other in the x direction
for i in range(0,10):
    particle_one[i,0] = i
    particle_two[i,0] = 2-i

    particle_one[i,1] = 2
    particle_two[i,1] = -2


particle_One_Radius = 1
particle_Two_Radius = 1.5

arc_Center = np.zeros((10,2))

for i in range(0,10):
    arc_Center[i,0] = (particle_one[i,0] + particle_two[i,0])/2


#the arc should disappear for frame 5
arc_Center[5,0] = 0
arc_Center[5,1] = 0

fig = plt.figure()
plt.axis([-20,20, -5,5]) #axis that I like
ax = plt.gca()

circle_One =     plt.Circle([particle_one[0,0],particle_one[0,1]],particle_One_Radius)
circle_Two = plt.Circle([particle_two[0,0],particle_two[0,1]],particle_Two_Radius)

circles = []

circles.append(circle_One)
circles.append(circle_Two)

arcs = []
#defines the arc
arc_one = patches.Arc([arc_Center[0,0],arc_Center[0,1]],5,3,angle =0 ,theta1 = 0,theta2= 270)
arcs.append(arc_one)

def init():
    ax.add_patch(circles[0])
    ax.add_patch(circles[1])
    ax.add_patch(arcs[0])
    return ax

#draw every frame by frame
def animate(m):

    circles[0].center=((particle_one[m,0],particle_one[m,1]))
    circles[1].center=((particle_two[m,0],particle_two[m,1]))

    #the arcs does not change
    arcs[0] =patches.Arc([arc_Center[m,0],arc_Center[m,1]],5+m,3+m,angle =0 ,theta1 = 0,theta2= 270)

    return ax
#animation function that draws 10 frames
anim = animation.FuncAnimation(fig,animate , init_func= init , frames = 10 , interval = 20)
plt.show()

圆的动画设置正确,但圆弧不会改变形状或位置

您的问题是,在创建圆时,您不是修改圆弧面片,而是在每一步创建一个新的面片,但在创建后不将其添加到轴

我简单地检查了一下,但我不知道如何修改Arc实例的属性,尽管我确信这是可能的

同时,我修改了您的函数,从面片列表中删除以前的圆弧,创建新圆弧,并将其添加回轴


我也遇到了我的弧不动的问题。尝试按照Diziet的建议删除圆弧会生成

错误:x不在列表中


然而,似乎有效的方法是实例化圆弧并将其添加到animate函数中的轴上,但不需要调用删除它-本质上,Diziet的解决方案减去了ax.patches.removearcs[0]行。

谢谢,现在解决了我的问题。我没有看到任何允许修改圆弧属性的内容。然而,我将需要使用更复杂的形状来绘制桥梁,所以这不是一个重要的问题。
#draw every frame by frame
def animate(m):
    circles[0].center=((particle_one[m,0],particle_one[m,1]))
    circles[1].center=((particle_two[m,0],particle_two[m,1]))
    ax.patches.remove(arcs[0])
    arcs[0] = patches.Arc([arc_Center[m,0],arc_Center[m,1]],5+m,3+m,angle =0 ,theta1 = 0,theta2= 270)
    ax.add_patch(arcs[0])
    print "step %d: arc = %s" % (m, arcs[0])
    return circles,arcs