我一直在使用matplotlib为某些图像制作动画,但是现在我发现我想向这些动画添加更多信息,因此我想覆盖一个指示重要特征的散点图。到目前为止,这是我用来生成电影的代码:
foreach (var file in d.GetFiles("*.pdf"))
{
Console.WriteLine(file.FullName);
Console.WriteLine(file.Name); // Without path
}
我想在每个帧的顶部添加一个散点图,就像我可以对这样的常规图做的那样:
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')
这会产生没有散点图的视频,所以我想知道如何正确实施此视频。
答案 0 :(得分:4)
docs说,使用blit=True
时,您必须从更新函数中返回“艺术家的可迭代作品”才能重画。但是,您仅返回img
。此外,您正在使用图像和散布对象覆盖img
。相反,您想要的是对散点使用不同的名称,例如
img = ax.imshow(frames[:,:,0], animated = True)
sct = ax.scatter(scatter[0],scatter[1],c='r',marker = '+')
两者都将在同一轴上绘制,但是现在您有img
和sct
艺术家,然后更新功能将是
def updatefig(img_num, img, sct, ax):
img.set_data(frames[:,:,img_num])
sct = ax.scatter(scatter[0], scatter[1], c='r', marker='+')
return [img, sct]