在使用 FuncAnimation 制作二维轨迹动画方面需要帮助
Need help on animating a 2-D trajectory using FuncAnimation
我有一个数组 x_trj
,其形状为 (50,3)
,我想使用该数组的第一列和第二列(分别为 x 和 y 坐标)绘制二维轨迹.这条轨迹将在一个圆的顶部。到目前为止,这是我的代码:
from matplotlib.animation import FuncAnimation
import matplotlib.pyplot as plt
fig = plt.figure()
ax = plt.axes(xlim=(-5, 5), ylim=(-5, 5))
line, = ax.plot([], [], lw=2)
# Plot circle
theta = np.linspace(0, 2*np.pi, 100)
plt.plot(r*np.cos(theta), r*np.sin(theta), linewidth=5)
ax = plt.gca()
def animate(n):
# Plot resulting trajecotry of car
for n in range(x_trj.shape[0]):
line.set_xdata(x_trj[n,0])
line.set_ydata(x_trj[n,1])
return line,
anim = FuncAnimation(fig, animate,frames=200, interval=20)
然而,动画结果是一个静止的人物。我查看了文档页面上的 Matplotlib 动画示例,但我仍然无法弄清楚在这种情况下我的 animate(n)
函数应该是什么样子。有人可以给我一些提示吗?
下面的代码做了如下改动:
- 添加了一些测试数据
- 在
animate
中:
- 删除
for
循环
- 只复制轨迹的一部分,直到给定的
n
- 在对
FuncAnimation
的调用中:
- `帧应该等于给定的点数(200 帧和 50 点效果不佳)
interval=
设置为更大的数字,因为 20 毫秒对于仅 50 帧来说太快了
- 添加了
plt.show()
(根据代码所在的环境运行,plt.show()
会触发动画开始)
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import numpy as np
# create some random test data
x_trj = np.random.randn(50, 3).cumsum(axis=0)
x_trj -= x_trj.min(axis=0, keepdims=True)
x_trj /= x_trj.max(axis=0, keepdims=True)
x_trj = x_trj * 8 - 4
fig = plt.figure()
ax = plt.axes(xlim=(-5, 5), ylim=(-5, 5))
line, = ax.plot([], [], lw=2)
# Plot circle
theta = np.linspace(0, 2 * np.pi, 100)
r = 4
ax.plot(r * np.cos(theta), r * np.sin(theta), linewidth=5)
def animate(n):
line.set_xdata(x_trj[:n, 0])
line.set_ydata(x_trj[:n, 1])
return line,
anim = FuncAnimation(fig, animate, frames=x_trj.shape[0], interval=200)
# anim.save('test_trajectory_animation.gif')
plt.show()
我有一个数组 x_trj
,其形状为 (50,3)
,我想使用该数组的第一列和第二列(分别为 x 和 y 坐标)绘制二维轨迹.这条轨迹将在一个圆的顶部。到目前为止,这是我的代码:
from matplotlib.animation import FuncAnimation
import matplotlib.pyplot as plt
fig = plt.figure()
ax = plt.axes(xlim=(-5, 5), ylim=(-5, 5))
line, = ax.plot([], [], lw=2)
# Plot circle
theta = np.linspace(0, 2*np.pi, 100)
plt.plot(r*np.cos(theta), r*np.sin(theta), linewidth=5)
ax = plt.gca()
def animate(n):
# Plot resulting trajecotry of car
for n in range(x_trj.shape[0]):
line.set_xdata(x_trj[n,0])
line.set_ydata(x_trj[n,1])
return line,
anim = FuncAnimation(fig, animate,frames=200, interval=20)
然而,动画结果是一个静止的人物。我查看了文档页面上的 Matplotlib 动画示例,但我仍然无法弄清楚在这种情况下我的 animate(n)
函数应该是什么样子。有人可以给我一些提示吗?
下面的代码做了如下改动:
- 添加了一些测试数据
- 在
animate
中:- 删除
for
循环 - 只复制轨迹的一部分,直到给定的
n
- 删除
- 在对
FuncAnimation
的调用中:- `帧应该等于给定的点数(200 帧和 50 点效果不佳)
interval=
设置为更大的数字,因为 20 毫秒对于仅 50 帧来说太快了
- 添加了
plt.show()
(根据代码所在的环境运行,plt.show()
会触发动画开始)
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import numpy as np
# create some random test data
x_trj = np.random.randn(50, 3).cumsum(axis=0)
x_trj -= x_trj.min(axis=0, keepdims=True)
x_trj /= x_trj.max(axis=0, keepdims=True)
x_trj = x_trj * 8 - 4
fig = plt.figure()
ax = plt.axes(xlim=(-5, 5), ylim=(-5, 5))
line, = ax.plot([], [], lw=2)
# Plot circle
theta = np.linspace(0, 2 * np.pi, 100)
r = 4
ax.plot(r * np.cos(theta), r * np.sin(theta), linewidth=5)
def animate(n):
line.set_xdata(x_trj[:n, 0])
line.set_ydata(x_trj[:n, 1])
return line,
anim = FuncAnimation(fig, animate, frames=x_trj.shape[0], interval=200)
# anim.save('test_trajectory_animation.gif')
plt.show()