python - Matplotlib animate 不更新刻度标签

标签 python animation matplotlib plot

我试图通过使动画在增加的 x 值上运行来修改和示例。我想根据 x 值更新 x 轴刻度标签。

我正在尝试使用 1.2 中的动画功能(特别是 FuncAnimation)。我可以设置 xlimit 但刻度标签没有更新。我也尝试过明确设置刻度标签,但这不起作用。

我看到了这个:Animating matplotlib axes/ticks和 我试图在 animation.py 中调整 bbox 但它没有用。我是 matplotlib 的新手,对解决这个问题的真正原因知之甚少,所以我将不胜感激任何帮助。

谢谢

"""
Matplotlib Animation Example

author: Jake Vanderplas
email: vanderplas@astro.washington.edu
website: http://jakevdp.github.com
license: BSD
Please feel free to use and modify this, but keep the above information. Thanks!
"""

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

# First set up the figure, the axis, and the plot element we want to animate
fig = plt.figure()
ax = plt.axes(ylim=(-2, 2))
line, = ax.plot([], [], lw=2)

# initialization function: plot the background of each frame
def init():
    line.set_data([], [])
    return line,

# animation function.  This is called sequentially
def animate(i):
    x = np.linspace(i, i+2, 1000)
    y = np.sin(2 * np.pi * (x - 0.01 * i))
    line.set_data(x, y)
    ax.set_xlim(i, i+2)

    return line,

# call the animator.  blit=True means only re-draw the parts that have changed.
anim = animation.FuncAnimation(fig, animate, init_func=init,
                           frames=200, interval=20, blit=True)

plt.show()

最佳答案

参见 Animating matplotlib axes/ticks , python matplotlib blit to axes or sides of the figure? , 和 Animated title in matplotlib

简单的答案是删除 blit=True

anim = animation.FuncAnimation(fig, animate, init_func=init,
                           frames=200, interval=20)

如果您有 blit = True,则只有更改的艺术家会被重新绘制(而不是重新绘制所有艺术家),这会提高渲染效率。如果从更新函数(在本例中为 animate)返回,艺术家将被标记为已更改。另一个细节是艺术家必须在轴边界框中使用代码在 animation.py 中的工作方式。有关如何处理此问题的信息,请参阅顶部的链接之一。

关于python - Matplotlib animate 不更新刻度标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14421924/

相关文章:

python - 函数逼近 : How is tile coding different from highly discretized state space?

python - 如何判断waf选择的工具?

jquery 简单的幻灯片

python - matplotlib安装ffmpeg需要了解什么

python - 设置与 pyplot.scatter 中的颜色匹配的图例

python - 如何获取并绘制信号包络线

python - 如果表单在 django for 循环中,如何检索 html 表单的输入值

python - 带有 monkeypatch 的 pytest fixture 参数

Android:上/下动画滑动菜单

JQUERY - 动画导航背景颜色