我一直在使用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)
这将生成一个没有散点图的视频,因此我想知道如何正确实现它。