3

我正在尝试创建一个 Matplotlib 动画,其中标题随每一帧而变化。这是我到目前为止所拥有的(几乎是从matplotlib.org盲目复制/粘贴);A包含我正在绘制的数据并textVec包含我要添加的标题:

fig = plt.figure()

textVec = ['Period ' + str(i[0]) + ' to ' + str(i[1]) + '.'
for i in sliceVec]

ims = []
for i in A:
   ims.append((ax = plt.pcolormesh(i), ))

plt.xlabel(r'$\omega$', size = 22)
plt.ylabel(r'$\gamma$', size = 22)

im_ani = animation.ArtistAnimation(fig, ims, interval=300,
   repeat_delay=1000, blit=True)

以上工作,但如何添加这些标题?

干杯

4

2 回答 2

1

老问题,但仍然花了我半天的时间尝试并尝试使用animation.ArtistAnimation(). 它只是不工作。
我得到的最接近的方法是设置注释,但它一位于绘图上方(不知何故在绘制区域之外)就消失了。

对我有用的animation.FuncAnimation()
是最初的问题中没有给出数据,所以我设置了一些东西。

# generate dummy data
data = []
for i in range(10):
    # make these smaller to increase the resolution
    dx, dy = 0.05, 0.05
    # generate 2 2d grids for the x & y bounds
    y, x = np.mgrid[slice(-5, 5 + dy, dy),
                    slice(-5, 5 + dx, dx)]   
    z = np.cos((x*x + y*y) - i*2*np.pi/10)
    # x and y are bounds, so z should be the value *inside* those bounds.
    # Therefore, remove the last value from the z array.
    z = z[:-1, :-1]
    
    data.append((x, y, z))
    
# generate title
titles = ["frame nr {}".format(frame) for frame in range(len(data))]


def func(frame, ax, data, titles):

    ax.pcolormesh(*data[frame])
    ax.set_title(titles[frame])
    
    return ax


fig = plt.figure()
ax = fig.add_subplot(111)

frames = range(len(data))
ani = animation.FuncAnimation(fig, func, frames, interval=300,
                              repeat_delay=1000, blit=False,
                              fargs=(ax, data, titles))
于 2020-09-22T18:11:39.437 回答
0

我没有在这里列出一个完整的解决方案,但是使用这些指针你应该能够得到你想要的:

您需要在函数update_line中包含应该在 python 代码中的一行(而不是在您的帖子中)。此行应在此函数的每次迭代中设置图形的标题。plt.title()为此目的应该可以正常工作。

于 2013-04-10T20:15:59.223 回答