Matplotlib animate不会更新刻度标签

Jar*_*red 3 python plot animation matplotlib

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

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

我看到了这个:动画matplotlib轴/刻度,我试图在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()
Run Code Online (Sandbox Code Playgroud)

tac*_*ell 5

请参阅动画matplotlib轴/刻度,python matplotlib blit到图的轴或侧面?,以及matplotlib中的动画标题

简单的答案是删除 blit=True

anim = animation.FuncAnimation(fig, animate, init_func=init,
                           frames=200, interval=20)
Run Code Online (Sandbox Code Playgroud)

如果您blit = True只有重新绘制的艺术家(而不是重新绘制所有艺术家),这使得渲染效率更高.如果从更新功能(在这种情况下animate)返回艺术家,则将其标记为已更改.另一个细节是艺术家必须在轴边界框中使用代码的工作方式animation.py.请参阅顶部的一个链接,了解如何处理此问题.

  • 谢谢你的回答。但这会重新绘制一切。我希望修复网格线和刻度线。我希望能够更改 x 轴标签上的文本 (2认同)