【发布时间】:2017-11-26 17:40:35
【问题描述】:
我正在尝试使用 matplotlib 为在 2D 空间中移动的一组点设置动画。它目前有点工作,因为它的代码确实产生了点的动画,但这不是我认为它会工作的方式。它不是在每个时间点绘制点,而是在所有时间点绘制它们。
例如,如果代码以 20 个点运行,我希望它在一帧显示 20 个点,然后在下一帧显示相同的点,依此类推。相反,它会保留以前的帧点,而不是只显示新的帧点。
谁能告诉我哪里出错了?
此外,我从研究中发现,最好为动画启用 blitting 以优化问题,但是当我添加 blit=True 作为 FuncAnimation 的参数时,控制台会吐出一个巨大的回溯,结尾为:
文件“C:\Users\Anaconda3\lib\site-packages\matplotlib\animation.py”,第 1568 行,在 _draw_frame a.set_animated(self._blit)
AttributeError: 'numpy.ndarray' 对象没有属性 'set_animated'
我也不知道为什么会发生这种情况,并且在线搜索没有帮助。
代码如下:
import matplotlib.pyplot as plt #Import plotting library
from matplotlib import animation
import numpy as np #Import numpy library
dim = 2 #Defines the dimensionality of the system
n = 25 #Number of BOIDS
tmax = 80 #Length of sim
dmax = 5 #Distance boids can "see", determines what other boids interact with them
o = np.zeros(dim) #Origin as vector
r = np.random.rand(n,dim) #Places BOIDs randomly with co-ordinates (x,y,z) from 0 to 1. Has dimensions n and dim
v = 2*np.random.rand(n,dim)-1#Sets initial velocity of each BOID from -1 to 1 in each cardinal direction
rt = np.zeros((tmax,n,dim)) #This array contains the whole system's positions at each point in time
x = np.empty(n)
y = np.empty(n)
d = np.zeros(n)
vk = np.zeros((n,2))
vksum = np.zeros((n,2))
pltx = np.zeros((tmax,n))
plty = np.zeros((tmax,n))
"""rt[a][b][0] is the x co-ordinate of boid n=b at t=a
rt[a][b][1] is the y co-ordiante of boid n=b at t=a
np.linalg.norm gives the modulus of an array, check documentation for arguments"""
fig, ax = plt.subplots(figsize=(14,9))
ax.grid(True,linestyle='-',color='0.75') #Sets up a grid on subplot
ax.set_xlim(-50,50)
ax.set_ylim(-50,50) #Set limits for x and y axes
for t in range (0,tmax):
for i in range (0,n):
for k in range (0,n):
if abs(k-n)>0:
d[k] = ((r[i][0]-r[k][0])**2+(r[i][1]-r[k][1])**2)**(1/2) #Checks distance from ith boid to each other boid
if (d[k]-dmax)<0: #If they are within range of the ith boid
vk[k] = (v[i] +v[k])/((np.linalg.norm(v[i]))*np.linalg.norm(v[k]))#Aligns the velocity of ith boid toward the velocity of the kth boid
for l in range (0,n):
vksum[i] = vksum[i] + vk[l] #Sums the boid's velocity contributions together
v[i] = (3/4)*v[i] + (vksum[i]/np.linalg.norm(vksum[i])) #Sets the boid's new velocity
r[i] = r[i] + v[i] #Sets the boid's new position
rt[t][i] = r[i] #Logs the position of the boid in the time array
pltx[t][i] = r[i][0]
plty[t][i] = r[i][1]
def init():
for i in range (0,n):
x[i] = rt[0][i][0]
y[i] = rt[0][i][1]
return x,y,
def update(j):
for i in range (0,n):
x[i] = rt[j][i][0]
y[i] = rt[j][i][1]
points = ax.scatter(x[:],y[:],c='r')
return x,y
anim = animation.FuncAnimation(fig, update, frames=tmax, interval=50,blit=True)
【问题讨论】:
-
也许在
update你应该清除情节以删除以前的点。
标签: python numpy animation matplotlib