Python:使用matplotlib.animation设置两个行列表的动画,但一次仅显示一组行

Python:使用matplotlib.animation设置两个行列表的动画,但一次仅显示一组行,python,matplotlib,animation,simultaneous,Python,Matplotlib,Animation,Simultaneous,尝试绘制带有螺旋线和收缩旋转多边形(每个当前点都有角)的persuit曲线 问题=无法同时绘制视线和主线 图形在收缩多边形(由视线描述)和主线路曲线(主线)之间来回摆动 当一次单独设置一个动画时,多边形和追踪曲线绘制得很好,但我无法让它们在同一个图形上协同工作 import numpy as np import matplotlib.pyplot as plt import matplotlib.animation as animation %matplotlib notebook plt.s

尝试绘制带有螺旋线和收缩旋转多边形(每个当前点都有角)的persuit曲线 问题=无法同时绘制视线和主线 图形在收缩多边形(由视线描述)和主线路曲线(主线)之间来回摆动

当一次单独设置一个动画时,多边形和追踪曲线绘制得很好,但我无法让它们在同一个图形上协同工作

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
%matplotlib notebook

plt.style.use('dark_background')


NumOfPoints = 6
deltaT = 0.005
duration = 50
steps = int(duration / deltaT)
speed = 0.2
num = 0

CurrentXPoints = []
CurrentYPoints = []
DeltaX = np.zeros(NumOfPoints)
DeltaY = np.zeros(NumOfPoints)
MagnitudeDelta = np.zeros(NumOfPoints)
VelocityX = np.zeros(NumOfPoints)
VelocityY = np.zeros(NumOfPoints)



  
#Creates Initial Points by equally spacing the points around a polygon inscribed around circle
for i in range(0,NumOfPoints): 
    x = np.cos(((i/NumOfPoints)*2)*np.pi)
    y = np.sin(((i/NumOfPoints)*2)*np.pi)

    CurrentXPoints.append(x)
    CurrentYPoints.append(y)

AllXPoints = np.array([CurrentXPoints])
AllYPoints = np.array([CurrentYPoints])

#Fills out both AllXPoints and AllYPoints with all points in duration 

for i in range(int(steps)):
    
    for j in range(0,NumOfPoints-1): #Calculates deltaX and deltaY at this timestep
        DeltaX[j] = CurrentXPoints[j+1] - CurrentXPoints[j]
        DeltaY[j] = CurrentYPoints[j+1] - CurrentYPoints[j]
    
    DeltaX[NumOfPoints-1] = CurrentXPoints[0] - CurrentXPoints[NumOfPoints-1]
    DeltaY[NumOfPoints-1] = CurrentYPoints[0] - CurrentYPoints[NumOfPoints-1]
    
    
    
    for j in range(0,NumOfPoints): # calculats new X and Y Points
        MagnitudeDelta[j] = ((DeltaX[j])**2 + (DeltaY[j])**2)**(1/2)
        VelocityX[j] = speed * (DeltaX[j]/MagnitudeDelta[j])
        VelocityY[j] = speed * (DeltaY[j]/MagnitudeDelta[j])
        CurrentXPoints[j] += deltaT * VelocityX[j]
        CurrentYPoints[j] += deltaT * VelocityY[j]

    CurrentXPointsArr = np.array(CurrentXPoints)
    CurrentYPointsArr = np.array(CurrentYPoints)
    
    AllXPoints = np.vstack((AllXPoints,CurrentXPointsArr))
    AllYPoints = np.vstack((AllYPoints,CurrentYPointsArr))



fig = plt.figure(figsize=(5,5))
ax = plt.axes()
ax.set_xlim(-2,2)
ax.set_ylim(-2,2)
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)


MainLines = []
SightLines= []
AllLines = MainLines + SightLines

for i in range(NumOfPoints):
    line, = ax.plot([AllXPoints[j][i] for j in range(steps)], [AllYPoints[j][i] for j in range(steps)])
    MainLines.append(line)
    SightLines.append(line)
  

 
def UpdateMain(num, AllXPoints, AllYPoints, MainLines):
    
    
    
    for line in MainLines:
    
        position = MainLines.index(line)
        line.set_data([AllXPoints[i][position] for i in range(num)], [AllYPoints[i][position] for i in range(num)])
        
    
    
def UpdateSight(num, AllXPoints, AllYPoints, SightLines):
    
    for line in SightLines:
        position = SightLines.index(line)

        if position < (NumOfPoints-1):
            line.set_data([AllXPoints[num][position],AllXPoints[num][position+1]],
                         [AllYPoints[num][position],AllYPoints[num][position+1]])
        else:
            line.set_data([AllXPoints[num][position],AllXPoints[num][0]],
                         [AllYPoints[num][position],AllYPoints[num][0]])

    
    
            
        
ani1 = animation.FuncAnimation(fig, UpdateMain,steps, fargs=[AllXPoints, AllYPoints, MainLines],
                  interval=1, blit=True)
          
ani2 = animation.FuncAnimation(fig, UpdateSight,steps, fargs=[AllXPoints, AllYPoints, SightLines],
                  interval=1, blit=True)


plt.show()
将numpy导入为np
将matplotlib.pyplot作为plt导入
将matplotlib.animation导入为动画
%matplotlib笔记本
plt.style.use('dark_background')
NumOfPoints=6
deltaT=0.005
持续时间=50
步长=整数(持续时间/增量)
速度=0.2
num=0
CurrentXPoints=[]
CurrentYPoints=[]
DeltaX=np.零(NumOfPoints)
DeltaY=np.零(NumOfPoints)
震级增量=np.零点(NumOfPoints)
VelocityX=np.零(NumOfPoints)
VelocityY=np.零(NumOfPoints)
#通过在围绕圆内接的多边形周围等距放置点来创建初始点
对于范围内的i(0,个点数):
x=np.cos(((i/NumOfPoints)*2)*np.pi)
y=np.sin(((i/NumOfPoints)*2)*np.pi)
CurrentXPoints.append(x)
CurrentYPoints.append(y)
AllXPoints=np.array([CurrentXPoints])
AllYPoints=np.array([CurrentYPoints])
#用持续时间内的所有点填写AllXPoints和AllYPoints
对于范围内的i(int(步长)):
对于范围(0,NumOfPoints-1)内的j:#计算此时间步的deltaX和deltaY
DeltaX[j]=CurrentXPoints[j+1]-CurrentXPoints[j]
DeltaY[j]=当前点[j+1]-当前点[j]
DeltaX[NumOfPoints-1]=CurrentXPoints[0]-CurrentXPoints[NumOfPoints-1]
DeltaY[NumOfPoints-1]=当前点[0]-当前点[NumOfPoints-1]
对于范围内的j(0,NumOfPoints):#计算新的X和Y点
震级Delta[j]=((DeltaX[j])**2+(DeltaY[j])**2)**(1/2)
速度x[j]=速度*(增量[j]/量级增量[j])
速度y[j]=速度*(增量[j]/量级增量[j])
CurrentXPoints[j]+=deltaT*VelocityX[j]
电流点[j]+=deltaT*VelocityY[j]
CurrentXpointsRR=np.array(CurrentXPoints)
CurrentyPointsRR=np.array(CurrentYPoints)
AllXPoints=np.vstack((AllXPoints,CurrentXPointsArr))
AllYPoints=np.vstack((AllYPoints,CurrentYPointsArr))
图=plt.图(图尺寸=(5,5))
ax=plt.axs()
ax.set_xlim(-2,2)
ax.set_ylim(-2,2)
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
主线=[]
视线=[]
所有线路=主线+视线
对于范围内的i(numof点):
直线,=ax.绘图([AllXPoints[j][i]表示范围内的j(步数)],[AllYPoints[j][i]表示范围内的j(步数)])
MainLines.append(行)
视线。附加(行)
def UpdateMain(num、AllXPoints、AllYPoints、MainLines):
对于干线中的线路:
位置=主线。索引(线)
行.设置_数据([AllXPoints[i][position]表示范围内的i(num)],[AllYPoints[i][position]表示范围内的i(num)])
def UpdateSight(num、AllXPoints、AllYPoints、SightLines):
对于视线中的直线:
位置=视线。索引(线)
如果位置<(数值点-1):
line.set_数据([AllXPoints[num][position],AllXPoints[num][position+1]),
[所有点[num][position],所有点[num][position+1]]
其他:
line.set_数据([AllXPoints[num][position],AllXPoints[num][0]],
[所有点[num][position],所有点[num][0]]
ani1=animation.FuncAnimation(图、UpdateMain、steps、fargs=[AllXPoints、AllYPoints、mainline],
间隔=1,blit=真)
ani2=animation.FuncAnimation(图,UpdateSight,steps,fargs=[AllXPoints,AllYPoints,SightLines],
间隔=1,blit=真)
plt.show()

首先,您应该只使用一个更新所有艺术家的
FuncAnimation
。 代码的主要问题是行

for i in range(NumOfPoints):
    line, = ax.plot([AllXPoints[j][i] for j in range(steps)], [AllYPoints[j][i] for j in range(steps)])
    MainLines.append(line)
    SightLines.append(line)
创建一个艺术家(
)并将其分配给两个不同的列表。如果为每个列表创建两个不同的行,则输出符合预期

完整工作代码:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
%matplotlib notebook

plt.style.use('dark_background')


NumOfPoints = 6
deltaT = 0.005
duration = 50
steps = int(duration / deltaT)
speed = 0.2
num = 0

CurrentXPoints = []
CurrentYPoints = []
DeltaX = np.zeros(NumOfPoints)
DeltaY = np.zeros(NumOfPoints)
MagnitudeDelta = np.zeros(NumOfPoints)
VelocityX = np.zeros(NumOfPoints)
VelocityY = np.zeros(NumOfPoints)


def update(num, AllXPoints, AllYPoints, MainLines, SightLines):
    out = []
    out.append(UpdateMain(num, AllXPoints, AllYPoints, MainLines))
    out.append(UpdateSight(num, AllXPoints, AllYPoints, SightLines))
    return out
 
def UpdateMain(num, AllXPoints, AllYPoints, MainLines):    
    for line in MainLines:
        position = MainLines.index(line)
        line.set_data([AllXPoints[i][position] for i in range(num)], [AllYPoints[i][position] for i in range(num)])
    return MainLines
        
    
    
def UpdateSight(num, AllXPoints, AllYPoints, SightLines): 
    for line in SightLines:
        position = SightLines.index(line)

        if position < (NumOfPoints-1):
            line.set_data([AllXPoints[num][position],AllXPoints[num][position+1]],
                         [AllYPoints[num][position],AllYPoints[num][position+1]])
        else:
            line.set_data([AllXPoints[num][position],AllXPoints[num][0]],
                         [AllYPoints[num][position],AllYPoints[num][0]])
    return SightLines

  
#Creates Initial Points by equally spacing the points around a polygon inscribed around circle
for i in range(0,NumOfPoints): 
    x = np.cos(((i/NumOfPoints)*2)*np.pi)
    y = np.sin(((i/NumOfPoints)*2)*np.pi)

    CurrentXPoints.append(x)
    CurrentYPoints.append(y)

AllXPoints = np.array([CurrentXPoints])
AllYPoints = np.array([CurrentYPoints])

#Fills out both AllXPoints and AllYPoints with all points in duration 
for i in range(int(steps)):
    for j in range(0,NumOfPoints-1): #Calculates deltaX and deltaY at this timestep
        DeltaX[j] = CurrentXPoints[j+1] - CurrentXPoints[j]
        DeltaY[j] = CurrentYPoints[j+1] - CurrentYPoints[j]

    DeltaX[NumOfPoints-1] = CurrentXPoints[0] - CurrentXPoints[NumOfPoints-1]
    DeltaY[NumOfPoints-1] = CurrentYPoints[0] - CurrentYPoints[NumOfPoints-1]

    for j in range(0,NumOfPoints): # calculats new X and Y Points
        MagnitudeDelta[j] = ((DeltaX[j])**2 + (DeltaY[j])**2)**(1/2)
        VelocityX[j] = speed * (DeltaX[j]/MagnitudeDelta[j])
        VelocityY[j] = speed * (DeltaY[j]/MagnitudeDelta[j])
        CurrentXPoints[j] += deltaT * VelocityX[j]
        CurrentYPoints[j] += deltaT * VelocityY[j]

    CurrentXPointsArr = np.array(CurrentXPoints)
    CurrentYPointsArr = np.array(CurrentYPoints)
    AllXPoints = np.vstack((AllXPoints,CurrentXPointsArr))
    AllYPoints = np.vstack((AllYPoints,CurrentYPointsArr))

fig = plt.figure(figsize=(5,5))
ax = plt.axes()
ax.set_xlim(-2,2)
ax.set_ylim(-2,2)
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)

MainLines = []
SightLines= []
AllLines = MainLines + SightLines

for i in range(NumOfPoints):
    line1, = ax.plot([AllXPoints[j][i] for j in range(steps)], [AllYPoints[j][i] for j in range(steps)])
    line2, = ax.plot([AllXPoints[j][i] for j in range(steps)], [AllYPoints[j][i] for j in range(steps)])
    MainLines.append(line1)
    SightLines.append(line2)

ani = animation.FuncAnimation(fig, update, steps, fargs=[AllXPoints, AllYPoints, MainLines, SightLines], interval=1, blit=True)

plt.show()
将numpy导入为np
将matplotlib.pyplot作为plt导入
将matplotlib.animation导入为动画
%matplotlib笔记本
plt.style.use('dark_background')
NumOfPoints=6
deltaT=0.005
持续时间=50
步长=整数(持续时间/增量)
速度=0.2
num=0
CurrentXPoints=[]
CurrentYPoints=[]
DeltaX=np.零(NumOfPoints)
DeltaY=np.零(NumOfPoints)
震级增量=np.零点(NumOfPoints)
VelocityX=np.零(NumOfPoints)
VelocityY=np.零(NumOfPoints)
def更新(num、AllXPoints、AllYPoints、主线、视线):
out=[]
append(UpdateMain(num、AllXPoints、AllYPoints、MainLines))
append(UpdateSight(num、AllXPoints、AllYPoints、SightLines))
返回
def UpdateMain(num、AllXPoints、AllYPoints、MainLines):
对于干线中的线路:
位置=主线。索引(线)
行.设置_数据([AllXPoints[i][position]表示范围内的i(num)],[AllYPoints[i][position]表示范围内的i(num)])
返回干线
def UpdateSight(num、AllXPoints、AllYPoints、SightLines):
对于视线中的直线:
位置=视线。索引(线)
如果位置<(数值点-1):
line.set_数据([AllXPoints[num][position],AllXPoints[num][position+1]),
[所有点[num][position],所有点[num][position+1]]
其他:
line.set_数据([AllXPoints[num][position],AllXPoints[num][0]],
[所有点[num][position],所有点[num][0]]
返回视线
#通过在围绕圆内接的多边形周围等距放置点来创建初始点
对于范围内的i(0,个点数):
x=np.cos((i/NumOfPoints)*2)*