在中动态更新标题和文本matplotlib.animation

2024-03-29 15:54:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我想制作一个动画matplotlib.animation动态更新分配给每个帧中轴的文本和标题。在

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

fig = plt.figure()
ax = plt.axes()

def func(year):
    # do something
    return data # a 1d numpy array for example

def plot_data(year):
    d = func(year)
    ax.set_title(year)
    ax.text(1, 2, year)
    return ax.bar(np.array([1,2]), d) # bar plot

bars = []
for year in years:
    bars.append(plot_data(year))

anim = animation.ArtistAnimation(fig, bars)
plt.show()

显示动画时,输出包含杂乱的标题和文本。有办法吗?在

如有任何帮助,我将不胜感激。在


Tags: 文本importnumpy标题dataplotmatplotlibas