1

下面是一个完全有效的代码,它为具有周期性边界的单车道道路设置动画。现在我想为两条甚至更多的车道设置动画。我有一个工作代码,其中位置向量现在是一个矩阵,每列代表一个车道。由此,我可以创建另一个 theta 向量,我希望在动画中的第一个向量之外显示它(通过将半径设置得更大一点)。我试图将矩阵(theta 和 r)放在 ax.scatter(theta, r, c=color) 中,但这不起作用。有人对如何解决这个问题有建议吗?我可以使用 matplotlib 的动画以外的其他东西,但由于它对单车道问题工作得很好,所以对我来说是最简单的。

把它包起来。如何同时为两个或多个矢量设置动画?例如,如果我有 r1、r2 和 theta1、theta2 并且想要每次都“绘制”它们,而不是像代码中那样只有 r 和 theta。

非常感谢您的帮助。

import numpy as np
from numpy.random import random as rnd
import matplotlib.pyplot as plt
from matplotlib import animation

roadlength = 50
numcars = 10
numframes = 1000  #Time
v_max = 5
p = 0.5

positions = np.zeros(numcars)
velocities = np.zeros(numcars)
theta = np.zeros(numcars)
color = np.linspace(0,numcars-1,numcars)

#Initiate r so roadlength = circumference of one lap
r = []
for i in range(numcars):
    r.append(roadlength/(2*np.pi))

#Initiate positions so the cars are spread out over the road
for i in range(1,numcars):
    positions[i] = positions[i-1] + (roadlength/numcars)

#Create figure        
fig = plt.figure()
ax = fig.add_subplot(111, projection='polar')
ax.axis('off')

#Update positions, animate function runs framenr times
def animate(framenr):
    positions_tmp = np.array(positions, copy=True)

    #Update position and velocity for each car
    for i in range(numcars):

        #Increase velocity if below max
        if velocities[i] < v_max:
                velocities[i] += 1
        #Decrease velocity if car in front is close
        d = positions_tmp[(i+1)%numcars] - positions_tmp[i]
        if d <= 0:
            d += roadlength
        if velocities[i] >= d:
            velocities[i] = d-1
        #Decrease velocity randomly
        if velocities[i] > 0:
            if rnd() < p:
                velocities[i] -= 1

        positions[i] = positions_tmp[i] + velocities[i]
        theta[i] = positions[i]*2*np.pi/roadlength

    return ax.scatter(theta, r, c=color),

# Call the animator, blit=True means only re-draw parts that have changed
anim = animation.FuncAnimation(fig, animate, frames=numframes, interval=100, blit=True, repeat=False)
plt.show()
4

1 回答 1

0

FuncAnimation()期望该animate()函数返回已在每一帧更新的艺术家数组。如果您需要为多个“情节”(或艺术家,或其他)设置动画,只需存储每个情节的结果,并返回所有变量的列表

此外,您的代码ax.scatter()在函数中重复调用animate,这会导致每次都绘制新点,并且绘制框架所需的时间会随着时间的推移而增加。我不知道这是否真的是你真正想做的。动画的最佳实践是在动画开始之前创建艺术家,然后在animate()函数内部更新它们的属性,而不是创建新的。

import numpy as np
from numpy.random import random as rnd
import matplotlib.pyplot as plt
from matplotlib import animation

roadlength = 50
numcars = 10
numframes = 1000  #Time
v_max = 5
p = 0.5

positions = np.zeros(numcars)
velocities = np.zeros(numcars)
theta = np.zeros(numcars)
color = np.linspace(0,numcars-1,numcars)

#Initiate r so roadlength = circumference of one lap
r = roadlength/(2*np.pi)

#Initiate positions so the cars are spread out over the road
for i in range(1,numcars):
    positions[i] = positions[i-1] + (roadlength/numcars)

#Create figure        
fig = plt.figure()
ax = fig.add_subplot(111, projection='polar')
ax.axis('off')

plot1 = ax.scatter(theta, [r]*numcars, c=color)
plot2 = ax.scatter(theta, [r+1]*numcars, c=color)

ax.set_ylim(0,r+2)

#Update positions, animate function runs framenr times
def animate(framenr):
    positions_tmp = np.array(positions, copy=True)

    #Update position and velocity for each car
    for i in range(numcars):

        #Increase velocity if below max
        if velocities[i] < v_max:
                velocities[i] += 1
        #Decrease velocity if car in front is close
        d = positions_tmp[(i+1)%numcars] - positions_tmp[i]
        if d <= 0:
            d += roadlength
        if velocities[i] >= d:
            velocities[i] = d-1
        #Decrease velocity randomly
        if velocities[i] > 0:
            if rnd() < p:
                velocities[i] -= 1

        positions[i] = positions_tmp[i] + velocities[i]
        theta[i] = positions[i]*2*np.pi/roadlength

    plot1.set_offsets(np.c_[theta, [r]*numcars])
    plot2.set_offsets(np.c_[theta, [r+1]*numcars])

    return [plot1, plot2]

# Call the animator, blit=True means only re-draw parts that have changed
anim = animation.FuncAnimation(fig, animate, frames=numframes, interval=100, blit=True, repeat=True)
plt.show()

在此处输入图像描述

于 2019-12-26T21:10:03.433 回答