生成数据:
import glob
import pandas as pd
import numpy as np
coords = dict()
for j in range(10):
x=np.random.uniform(0.0, 5.0,2);
y=np.random.uniform(0.0, 5.0,2);
vx=np.random.uniform(-1,1,2);
vy=np.random.uniform(-1,1,2);
coords[j]= {'x': x, 'y':y, 'vx':vx, 'vy':vy}
绘图部分:
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
from matplotlib import animation, rc
from IPython.display import HTML
%matplotlib inline
fig, ax = plt.subplots(1,1)
ax.set_xlim(-5, 5)
ax.set_ylim(-5, 5)
def update_quiver(frameIdx):
global coords
frame = coords[frameIdx]
X = frame['x']
Y = frame['y']
U = frame['vx']
V = frame['vy']
Q = ax.quiver(X, Y, U, V, pivot='mid', color='k', units='inches')
Q.set_UVC(U,V)
return Q,
然后绘图:
rc('animation', html='jshtml')
anim = animation.FuncAnimation(fig, update_quiver, fargs=(),
interval=50, blit=False, frames=max(coords.keys()))
anim
我如何仅可视化粒子的当前位置而不是整个轨迹?
当我尝试:
def update_quiver(frameIdx):
global coords
fig.clf()
ax.set_xlim(-5, 5)
ax.set_ylim(-5, 5)
frame = coords[frameIdx]
X = frame['x']
Y = frame['y']
U = frame['vx']
V = frame['vy']
Q = ax.quiver(X, Y, U, V, pivot='mid', color='k', units='inches')
Q.set_UVC(U,V)
return Q,
它什么都没显示。
答案 0 :(得分:1)
删除我评论过的颤抖的选项可能如下所示:
import numpy as np; np.random.seed(1)
coords = dict()
for j in range(10):
x=np.random.uniform(0.0, 5.0,2);
y=np.random.uniform(0.0, 5.0,2);
vx=np.random.uniform(-1,1,2);
vy=np.random.uniform(-1,1,2);
coords[j]= {'x': x, 'y':y, 'vx':vx, 'vy':vy}
import numpy as np
from matplotlib import pyplot as plt, animation
fig, ax = plt.subplots(1,1)
ax.set_xlim(-1, 6)
ax.set_ylim(-1, 6)
Q = ax.quiver(1,1,1,1, alpha=0)
def update_quiver(frameIdx):
global coords, Q
frame = coords[frameIdx]
X = frame['x']
Y = frame['y']
U = frame['vx']
V = frame['vy']
Q.remove()
Q = ax.quiver(X, Y, U, V, pivot='mid', color='k', units='inches')
return Q,
anim = animation.FuncAnimation(fig, update_quiver, fargs=(),
interval=500, blit=False, frames=max(coords.keys()))
plt.show()
答案 1 :(得分:-1)
您可以尝试清除update_quiver
中的数字:
fig.clf()
编辑:此代码在我的计算机上有效:
def update_quiver(frameIdx):
frame = coords[frameIdx]
plt.cla()
ax.set_xlim(-1, 7)
ax.set_ylim(-1, 7)
X = frame['x']
Y = frame['y']
U = frame['vx']
V = frame['vy']
Q = ax.quiver(X, Y, U, V, pivot='mid', color='k', units='inches')
Q.set_UVC(U,V)
return Q,
anim = animation.FuncAnimation(fig, update_quiver, fargs=(),interval=500, blit=True,frames=np.arange(len(coords)))
fig.tight_layout()
plt.show()