python – 使用matplotlib ArtistAnimation为动画添加文本
发布时间:2020-12-20 11:36:01 所属栏目:Python 来源:网络整理
导读:我有几个图像作为2D阵列,我想创建这些图像的动画,并添加随图像变化的文本. 到目前为止,我设法得到动画,但我需要你的帮助,为每个图像添加一个文本. 我有一个for循环来打开每个图像并将它们添加到动画中,并且假设我想要为每个图像添加图像编号(imgNum). 这是我
我有几个图像作为2D阵列,我想创建这些图像的动画,并添加随图像变化的文本.
到目前为止,我设法得到动画,但我需要你的帮助,为每个图像添加一个文本. 我有一个for循环来打开每个图像并将它们添加到动画中,并且假设我想要为每个图像添加图像编号(imgNum). 这是我的代码,用于生成图像的电影,没有文本. ims = [] fig = plt.figure("Animation") ax = fig.add_subplot(111) for imgNum in range(numFiles): fileName= files[imgNum] img = read_image(fileName) frame = ax.imshow(img) ims.append([frame]) anim = animation.ArtistAnimation(fig,ims,interval=350,blit=True,repeat_delay=350) anim.save('dynamic_images.mp4',fps = 2) plt.show() 那么,我如何用imgNum为每个图像添加一个文本? 谢谢你的帮助! 解决方法
您可以使用
annotate添加文本,并将注释艺术家添加到您传递给
ArtistAnimation的列表艺术家.以下是基于您的代码的示例.
import matplotlib.pyplot as plt from matplotlib import animation import numpy as np ims = [] fig = plt.figure("Animation") ax = fig.add_subplot(111) for imgNum in range(10): img = np.random.rand(10,10) #random image for an example frame = ax.imshow(img) t = ax.annotate(imgNum,(1,1)) # add text ims.append([frame,t]) # add both the image and the text to the list of artists anim = animation.ArtistAnimation(fig,repeat_delay=350) plt.show() (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
热点阅读