如何使用动画生成动画曲线?
How to generate an animated curve using animation?
我想使用某些特定的点生成动画曲线。但是,当我使用下面的代码时,没有显示动画,图片的 x 坐标和 y 坐标不断变化。我使用 FuncAnimation
函数,我传递给这个函数的参数是 plot_gesture
,绘制所有点的函数。
我的轨迹是一个列表,它的元素都是列表(长度为2),就像[[2.5,2], [4,3.5], [2,6]....]
我的位置是一个ndarray,它的元素都是元组(长度为2),就像((3,5),(6,7)......
轨迹的长度比位置长很多,因为位置是轨迹采样点的所有点。轨迹是整个轨迹的所有点。所以,我需要画出轨迹上的所有点,并将位置的点打散
def plot_gesture(i = int):
X = []
Y = []
X_point = []
Y_point = []
for x in trajectory[:i]:
X.append(x[0])
Y.append(x[1])
for x in position[:i]:
X_point.append(x[0])
Y_point.append(x[1])
plt.plot(X, Y)
for x, y in zip(X_point, Y_point):
plt.scatter(x, y)
if __name__ == "__main__":
trajectory = point_list
position = np.array(sampled_all_points)
ani=animation.FuncAnimation(fig= fig,func= plot_gesture, interval= 20)
plt.show();
您应该始终将您的数据包含在您的问题中。
由于您没有指定 position
和 trajectory
是什么,我想您需要一个 position
具有 N
行和 2 列的数组,其中包含 x
和y
移动点的坐标。我生成了一个这样的数组:
N = 200
time = np.linspace(0, 30, N)
position = np.array([1/3*time*np.cos(time), 1/3*time*np.sin(time)]).T
[[ 0.00000000e+00 0.00000000e+00]
[ 4.96813143e-02 7.54690426e-03]
[ 9.59688338e-02 2.98452338e-02]
[ 1.35597167e-01 6.58794885e-02]
[ 1.65553654e-01 1.13995649e-01]
[ 1.83194645e-01 1.71957672e-01]
[ 1.86350032e-01 2.37024178e-01]
[ 1.73412613e-01 3.06042995e-01]
[ 1.43409393e-01 3.75560683e-01]
[ 9.60523732e-02 4.41943697e-01]
[ 3.17670638e-02 5.01507457e-01]
...
您需要定义一个图形以传递给 FuncAnimation
和一个 animation
函数。在此功能中,您需要:
- 删除之前的情节
- 绘制当前状态
- 修复轴以避免在动画过程中不愉快的轴修改
所以你可以这样设置代码:
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import numpy as np
def animate(i):
# erase previous plot
ax.cla()
# draw point's trajectory
ax.plot(position[:i + 1, 0], position[:i + 1, 1], linestyle = '-', color = 'blue')
# draw point's current position
ax.plot(position[i, 0], position[i, 1], marker = 'o', markerfacecolor = 'red', markeredgecolor = 'red')
# fix axes limits
ax.set_xlim(-10, 10)
ax.set_ylim(-10, 10)
if __name__ == "__main__":
# position array generation
N = 200
time = np.linspace(0, 30, N)
position = np.array([1/3*time*np.cos(time), 1/3*time*np.sin(time)]).T
# generate figure and axis
fig, ax = plt.subplots(figsize = (5, 5))
# define the animation
ani = FuncAnimation(fig = fig, func = animate, interval = 20, frames = N)
# show the animation
plt.show()
我想使用某些特定的点生成动画曲线。但是,当我使用下面的代码时,没有显示动画,图片的 x 坐标和 y 坐标不断变化。我使用 FuncAnimation
函数,我传递给这个函数的参数是 plot_gesture
,绘制所有点的函数。
我的轨迹是一个列表,它的元素都是列表(长度为2),就像[[2.5,2], [4,3.5], [2,6]....] 我的位置是一个ndarray,它的元素都是元组(长度为2),就像((3,5),(6,7)...... 轨迹的长度比位置长很多,因为位置是轨迹采样点的所有点。轨迹是整个轨迹的所有点。所以,我需要画出轨迹上的所有点,并将位置的点打散
def plot_gesture(i = int):
X = []
Y = []
X_point = []
Y_point = []
for x in trajectory[:i]:
X.append(x[0])
Y.append(x[1])
for x in position[:i]:
X_point.append(x[0])
Y_point.append(x[1])
plt.plot(X, Y)
for x, y in zip(X_point, Y_point):
plt.scatter(x, y)
if __name__ == "__main__":
trajectory = point_list
position = np.array(sampled_all_points)
ani=animation.FuncAnimation(fig= fig,func= plot_gesture, interval= 20)
plt.show();
您应该始终将您的数据包含在您的问题中。
由于您没有指定 position
和 trajectory
是什么,我想您需要一个 position
具有 N
行和 2 列的数组,其中包含 x
和y
移动点的坐标。我生成了一个这样的数组:
N = 200
time = np.linspace(0, 30, N)
position = np.array([1/3*time*np.cos(time), 1/3*time*np.sin(time)]).T
[[ 0.00000000e+00 0.00000000e+00]
[ 4.96813143e-02 7.54690426e-03]
[ 9.59688338e-02 2.98452338e-02]
[ 1.35597167e-01 6.58794885e-02]
[ 1.65553654e-01 1.13995649e-01]
[ 1.83194645e-01 1.71957672e-01]
[ 1.86350032e-01 2.37024178e-01]
[ 1.73412613e-01 3.06042995e-01]
[ 1.43409393e-01 3.75560683e-01]
[ 9.60523732e-02 4.41943697e-01]
[ 3.17670638e-02 5.01507457e-01]
...
您需要定义一个图形以传递给 FuncAnimation
和一个 animation
函数。在此功能中,您需要:
- 删除之前的情节
- 绘制当前状态
- 修复轴以避免在动画过程中不愉快的轴修改
所以你可以这样设置代码:
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import numpy as np
def animate(i):
# erase previous plot
ax.cla()
# draw point's trajectory
ax.plot(position[:i + 1, 0], position[:i + 1, 1], linestyle = '-', color = 'blue')
# draw point's current position
ax.plot(position[i, 0], position[i, 1], marker = 'o', markerfacecolor = 'red', markeredgecolor = 'red')
# fix axes limits
ax.set_xlim(-10, 10)
ax.set_ylim(-10, 10)
if __name__ == "__main__":
# position array generation
N = 200
time = np.linspace(0, 30, N)
position = np.array([1/3*time*np.cos(time), 1/3*time*np.sin(time)]).T
# generate figure and axis
fig, ax = plt.subplots(figsize = (5, 5))
# define the animation
ani = FuncAnimation(fig = fig, func = animate, interval = 20, frames = N)
# show the animation
plt.show()