【发布时间】:2021-11-14 20:48:30
【问题描述】:
我想运行一个动画,其中可以看到一个“点”移动并在后面留下一个“痕迹”。绘制整个轨迹后,我想将数据(axis.scatter)绘制到背景。现在它首先绘制散点图并在其上绘制线条。
figure, axis = plt.subplots()
point, = axis.plot([], [], zorder=3)
trace, = axis.plot([], [], zorder=2)
x_s_reduced = x_s[300::300] # len(x_s) = 5e5
y_s_reduced = y_s[300::300] # len(y_s) = 5e5
sampling_period = sampling_period * 300 # sampling_period = 1e-5
#
maxlen = len(x_s_reduced)
history_x, history_y = deque(maxlen=maxlen), deque(maxlen=maxlen)
def update(i):
if i == 0:
history_x.clear()
history_y.clear()
history_x.appendleft(x_s_reduced[i])
history_y.appendleft(y_s_reduced[i])
point.set_data(x_s_reduced[i], y_s_reduced[i])
trace.set_data(history_x, history_y)
#if i == maxlen:
#scatter = axis.scatter(x_s, y_s, color='lightgrey', zorder=1)
#return point, trace, time_text, scatter
return point, trace, time_text
#
beam_video = animation.FuncAnimation(figure, update, frames=maxlen, interval=0.001, repeat=False, blit=True)
axis.scatter(x_s, y_s, color='lightgrey', zorder=1)
plt.show()
我已经尝试将以下内容添加到更新功能中,但不起作用:
...
if i == maxlen:
scatter = axis.scatter(x_s, y_s, color='lightgrey', zorder=1)
return point, trace, time_text, scatter
...
有没有办法等待动画停止然后添加散点图? 还是我必须找到一种方法将其合并到更新功能中?
【问题讨论】:
标签: python matplotlib animation scatter-plot