matplotlib艺术家动画:标题或文本不变 [英] matplotlib artist animation : title or text not changing
问题描述
我尝试使用matplotlib的ArtistAnimation.该图的文本和标题应该在每个框架中都发生变化,但它们不会发生变化. 我已经阅读了很多有关类似问题的文章,但我仍然不知道解决方案是什么.据我所知,这些演示没有显示更新的标题.
I tried using matplotlib's ArtistAnimation. The text and titles of the figure are supposed to change in each frame, but they don't. I have read tons of posts on similar problems, but I still don't understand what is the solution. The demos don't show updating titles as far as I could find.
如果外面有人知道,我将不胜感激!
If anybody out there knows, I would be grateful!
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.animation as animation
fig =plt.figure()
ims=[]
for iternum in range(4):
plt.title(iternum)
plt.text(iternum,iternum,iternum)
ims.append([plt.scatter(np.random.randint(0,10,5), np.random.randint(0,20,5),marker='+' )])
#plt.cla()
ani = animation.ArtistAnimation(fig, ims, interval=500, blit=False,
repeat_delay=2000)
plt.show()
推荐答案
要使艺术家动起来,必须返回对ims[]
数组中每个艺术家的引用,包括Text
对象.
To animate artists, you have to return a reference to each artists in your ims[]
array, including the Text
objects.
但这不适用于标题,我不知道为什么.也许对所涉及机制有更深入了解的人将能够启发我们.
However it doesn't work for the title, I don't know why. Maybe somebody with a better understanding of the mechanisms involved will be able to enlighten us.
尽管如此,标题只是一个Text
对象,所以我们可以使用以下方法产生想要的效果:
Nevertheless, the title is just a Text
object, so we can produce the desired effect using:
fig = plt.figure()
ax = fig.add_subplot(111)
ims=[]
for iternum in range(4):
ttl = plt.text(0.5, 1.01, iternum, horizontalalignment='center', verticalalignment='bottom', transform=ax.transAxes)
txt = plt.text(iternum,iternum,iternum)
ims.append([plt.scatter(np.random.randint(0,10,5), np.random.randint(0,20,5),marker='+' ), ttl, txt])
#plt.cla()
ani = animation.ArtistAnimation(fig, ims, interval=500, blit=False,
repeat_delay=2000)
这篇关于matplotlib艺术家动画:标题或文本不变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!