gpt4 book ai didi

python - 如何使用animation.FuncAnimation仅绘制当前帧?

转载 作者:太空宇宙 更新时间:2023-11-03 21:07:42 25 4
gpt4 key购买 nike

生成数据:

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

enter image description here

如何仅可视化粒子的当前位置而不是整个轨迹?

当我尝试时:

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,

它没有显示任何内容。

最佳答案

删除我评论的箭袋的选项可能如下所示:

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()

关于python - 如何使用animation.FuncAnimation仅绘制当前帧?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55289183/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com