【发布时间】:2019-12-13 21:04:43
【问题描述】:
我一直在使用 matplotlib 为一些图像制作动画,但我现在发现我想为这些动画添加更多信息,所以我想覆盖一个散点图来指示重要特征。到目前为止,这是我用来生成电影的代码:
def make_animation(frames,path,name):
plt.rcParams['animation.ffmpeg_path'] = u'/Users/~/anaconda3/bin/ffmpeg' #ffmpeg path
n_images=frames.shape[2]
assert (n_images>1)
figsize=(10,10)
fig, ax = plt.subplots(figsize=figsize)
fig.tight_layout()
fig.subplots_adjust(left=0, bottom=0, right=1, top=1, wspace=None, hspace=None)
#lineR, = ax.plot(xaxis_data[0],R_data[0],'c-',label="resources")
img = ax.imshow(frames[:,:,0], animated = True)
def updatefig(img_num):
#lineR.set_data(xaxis_data[img_num],R_data[img_num],'r-')
img.set_data(frames[:,:,img_num])
return [img]
ani = animation.FuncAnimation(fig, updatefig, np.arange(1, n_images), interval=50, blit=True)
mywriter = animation.FFMpegWriter(fps = 20)
#ani.save('mymovie.mp4',writer=mywriter)
ani.save("/Users/~/output/"+ path + "/" + name + ".mp4",writer=mywriter)
plt.close(fig)
我想在每一帧的顶部添加一个散点图,就像我可以使用这样的常规图:
fig, ax = plt.subplots()
img = ax.imshow(frames[:,:,0])
img = ax.scatter(scatter_pts[0],scatter_pts[1],marker='+',c='r')
我的第一次尝试是这样的:
def make_animation_scatter(frames,path,name,scatter):
plt.rcParams['animation.ffmpeg_path'] = u'/Users/~/anaconda3/bin/ffmpeg' #ffmpeg path
n_images=frames.shape[2]
assert (n_images>1)
figsize=(10,10)
fig, ax = plt.subplots(figsize=figsize)
fig.tight_layout()
fig.subplots_adjust(left=0, bottom=0, right=1, top=1, wspace=None, hspace=None)
#lineR, = ax.plot(xaxis_data[0],R_data[0],'c-',label="resources")
img = ax.imshow(frames[:,:,0], animated = True)
img = ax.scatter(scatter[0],scatter[1],c='r',marker = '+')
def updatefig(img_num):
#lineR.set_data(xaxis_data[img_num],R_data[img_num],'r-')
img.set_data(frames[:,:,img_num])
img = ax.scatter(scatter[0],scatter[1],c='r',marker = '+')
return [img]
ani = animation.FuncAnimation(fig, updatefig, np.arange(1, n_images), interval=50, blit=True)
mywriter = animation.FFMpegWriter(fps = 20)
#ani.save('mymovie.mp4',writer=mywriter)
ani.save("/Users/~/output/"+ path + "/" + name + ".mp4",writer=mywriter)
plt.close(fig)
这会产生一个没有散点图的视频,所以我想知道如何正确实现它。
【问题讨论】:
-
“它不起作用”到底是什么意思?
-
它生成的视频只有帧而不是散点图。我已将问题编辑得更清楚,感谢您的提问。
-
下面的答案能解决这个问题吗? (如果有,请采纳)
-
是的,我之前没有时间测试它。
标签: python matplotlib animation