如何为 Python matplotlib.animation 添加 time-varying 标题?
How to add time-varying title for Python matplotlib.animation?
对不起我的英语不好。
我有一个矩阵 datas
(10000 乘以 5000)。包括10000个案例数据,每个数据的维度为5000。
我想制作一个动画来一个接一个地展示每一个数据
遵循代码 1 效果很好。
(代码 1)
import matplotlib.pyplot as plt
import matplotlib.animation as animation
fig = plt.figure()
ims = []
for i in range(10000):
im = plt.plot(masks[i,:])
ims.append(im)
ani = animation.ArtistAnimation(fig, ims, interval=10)
plt.show()
ani.save('output.mp4', writer="ffmpeg")
我想添加time-varying标题,知道某个时间显示的是哪些数据(数据索引)
然后我写了下面的代码2。
(代码2)
import matplotlib.pyplot as plt
import matplotlib.animation as animation
fig = plt.figure()
ims = []
for i in range(10000):
im = plt.plot(masks[i,:])
tl = 'Data number:' + str(i+1) # ***added***
plt.title(tl) # ***added***
ims.append(im)
ani = animation.ArtistAnimation(fig, ims, interval=10)
plt.show()
ani.save('output.mp4', writer="ffmpeg")
但是,我得到了一个动画,它的标题总是 'Data number: 10000'。
如何编写代码来添加 time-varying 标题?
我在 im = plt.plot(masks[i,:])
之前写了 plt.title(tl)
但没有任何改变。谢谢你的帮助。
我的环境是;
- Python 3.6.9
- matplitlib 3.3.3
我们可以通过标注一个axes对象来模仿图题:
#test data generation
import numpy as np
np.random.seed(123)
masks = np.random.randn(10, 15)
#the animation routine starts here
import matplotlib.pyplot as plt
import matplotlib.animation as animation
fig, ax = plt.subplots()
ims = []
#iterating over the array
for i in range(masks.shape[0]):
#obtaining the Line2D object representing the line plot
im, = ax.plot(masks[i,:], color="blue")
#creating a centered annotation text above the graph
ann = ax.annotate(f"This is frame {i:.0f}.", (0.5, 1.03), xycoords="axes fraction", ha="center")
#collecting both objects for the animation
ims.append([im, ann])
ani = animation.ArtistAnimation(fig, ims, interval=300, repeat=False)
plt.show()
对不起我的英语不好。
我有一个矩阵 datas
(10000 乘以 5000)。包括10000个案例数据,每个数据的维度为5000。
我想制作一个动画来一个接一个地展示每一个数据
遵循代码 1 效果很好。
(代码 1)
import matplotlib.pyplot as plt
import matplotlib.animation as animation
fig = plt.figure()
ims = []
for i in range(10000):
im = plt.plot(masks[i,:])
ims.append(im)
ani = animation.ArtistAnimation(fig, ims, interval=10)
plt.show()
ani.save('output.mp4', writer="ffmpeg")
我想添加time-varying标题,知道某个时间显示的是哪些数据(数据索引)
然后我写了下面的代码2。
(代码2)
import matplotlib.pyplot as plt
import matplotlib.animation as animation
fig = plt.figure()
ims = []
for i in range(10000):
im = plt.plot(masks[i,:])
tl = 'Data number:' + str(i+1) # ***added***
plt.title(tl) # ***added***
ims.append(im)
ani = animation.ArtistAnimation(fig, ims, interval=10)
plt.show()
ani.save('output.mp4', writer="ffmpeg")
但是,我得到了一个动画,它的标题总是 'Data number: 10000'。
如何编写代码来添加 time-varying 标题?
我在 im = plt.plot(masks[i,:])
之前写了 plt.title(tl)
但没有任何改变。谢谢你的帮助。
我的环境是;
- Python 3.6.9
- matplitlib 3.3.3
我们可以通过标注一个axes对象来模仿图题:
#test data generation
import numpy as np
np.random.seed(123)
masks = np.random.randn(10, 15)
#the animation routine starts here
import matplotlib.pyplot as plt
import matplotlib.animation as animation
fig, ax = plt.subplots()
ims = []
#iterating over the array
for i in range(masks.shape[0]):
#obtaining the Line2D object representing the line plot
im, = ax.plot(masks[i,:], color="blue")
#creating a centered annotation text above the graph
ann = ax.annotate(f"This is frame {i:.0f}.", (0.5, 1.03), xycoords="axes fraction", ha="center")
#collecting both objects for the animation
ims.append([im, ann])
ani = animation.ArtistAnimation(fig, ims, interval=300, repeat=False)
plt.show()