Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib animate does not update tick labels

I am trying to modify and example by making the animation run on increasing x values. I want update the x axis tick labels to update according to the x values.

I am trying to use the animation features (specifically FuncAnimation) in 1.2. I can set the xlimit but the tick labels are not updating. I tried explicitly setting the tick labels too and this does not work.

I saw this: Animating matplotlib axes/ticks and I tried to adjust the bbox in animation.py but it did not work. I am fairly new to matplotlib and do not know enough about what is really going on to address this issue so I would appreciate any help.

Thank you

"""
Matplotlib Animation Example

author: Jake Vanderplas
email: [email protected]
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()
like image 376
Jared Avatar asked Jan 20 '13 05:01

Jared


1 Answers

See Animating matplotlib axes/ticks, python matplotlib blit to axes or sides of the figure?, and Animated title in matplotlib

The simple answer is remove blit=True

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

If you have blit = True only artists that have changed are re-drawn (rather than re-drawing all of the artists) which makes the rendering more efficient. Artists are marked as changed if they are returned from the update-function (in this case animate). The other detail is that the artists must be with in the axes bounding box with the way the code works in animation.py. See one of the links at the top for how to deal with this.

like image 103
tacaswell Avatar answered Nov 17 '22 14:11

tacaswell