Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:在Matplotlib动画中添加动态文本_Python_Matplotlib - Fatal编程技术网

Python:在Matplotlib动画中添加动态文本

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

我从保存为numpy数组的图像列表中制作了一个动画。 然后我想在动画中添加一个文本,就像字幕一样,每帧的文本都会发生变化,但是放置
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())