Python:在Matplotlib动画中添加动态文本
我从保存为numpy数组的图像列表中制作了一个动画。 然后我想在动画中添加一个文本,就像字幕一样,每帧的文本都会发生变化,但是放置Python:在Matplotlib动画中添加动态文本,python,matplotlib,Python,Matplotlib,我从保存为numpy数组的图像列表中制作了一个动画。 然后我想在动画中添加一个文本,就像字幕一样,每帧的文本都会发生变化,但是放置plt.text(一些字符串)只会在第一次迭代时添加字符串,如果我在循环中更改传递的字符串,它就不起作用。下面是我的尝试。请注意HTML仅适用于Jupyter实验室 import matplotlib.animation as animation from PIL import Image from IPython.display import HTML import
plt.text(一些字符串)
只会在第一次迭代时添加字符串,如果我在循环中更改传递的字符串,它就不起作用。下面是我的尝试。请注意HTML仅适用于Jupyter实验室
import matplotlib.animation as animation
from PIL import Image
from IPython.display import HTML
import matplotlib.pyplot as plt
folderName = "hogehoge"
picList = glob.glob(folderName + "\*.npy")
fig = plt.figure()
ims = []
for i in range(len(picList)):
plt.text(10, 10, i) # This does not add the text properly
tmp = Image.fromarray(np.load(picList[i]))
ims.append(plt.imshow(tmp))
ani = animation.ArtistAnimation(fig, ims, interval=200)
HTML(ani.to_jshtml())
您还必须将文本对象添加到每个帧的艺术家列表中:
将matplotlib.animation导入为动画
从PIL导入图像
从IPython.display导入HTML
将matplotlib.pyplot作为plt导入
将numpy作为np导入
图,ax=plt.子批次()
ims=[]
对于范围(10)内的i:
艺术家=轴图(np.random.rand(10),np.random.rand(10))
text=ax.text(x=0.5,y=0.5,s=i)
艺术家。追加(文本)
ims.append(艺术家)
ani=动画。艺术动画(图,ims,间隔=200)
HTML(ani.to_jshtml())