2

How can I get the tick labels to update once changed in an animated graph? Here is just a simple example of what I need. I realize I can set blit=False and have it work, however the project I am working on requires blit=True for performance reasons.

import matplotlib
matplotlib.use('Qt4Agg')
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.animation as animation
import time

fig, ax = plt.subplots()

x = np.arange(0, 2*np.pi, 0.01)
line, = ax.plot(x, np.sin(x))


def animate(i):
    line.set_ydata(np.sin(x + i/10.0))

    if i > 30:
        ax.tick_params(axis='y', colors='red')  ### How do I get my graph to reflect this change?
    return line,


def init():
    line.set_ydata(np.ma.array(x, mask=True))
    return line,

ani = animation.FuncAnimation(fig, animate, np.arange(1, 200), init_func=init,
                          interval=100, blit=True)
plt.show()
HexxNine
  • 446
  • 9
  • 23
  • 1
    you need to find the artist for the ticks and return it in your init and animate functions like you do for the line artist – Aaron Feb 15 '17 at 19:57
  • @Aaron Thank you Aaron, would you by chance know what artist that would be or where to find the answer? Ive been looking at the docs but I have not been able to figure that out yet. – HexxNine Feb 15 '17 at 20:10
  • I'm not really sure.. the script actually works as expected for me, but I'm using qt5 – Aaron Feb 15 '17 at 20:16
  • @Aaron you're saying the tick labels change color on their own for you? hmm, they only change for me when I adjust the size of the window, I guessing because it has to redraw the whole graph again to fit the new window size...but mine don't change on their own...weird. – HexxNine Feb 15 '17 at 20:21
  • maybe [this](http://matplotlib.org/api/axes_api.html#ticks-and-tick-labels) with [`ax.get_yticklabels()`](http://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.get_yticklabels.html#matplotlib.axes.Axes.get_yticklabels) – Aaron Feb 15 '17 at 20:27
  • @Aaron I tried this earlier but got `AttributeError: 'silent_list' object has no attribute 'set_animated'` – HexxNine Feb 15 '17 at 20:35
  • Possible duplicate of [Animated title in matplotlib](http://stackoverflow.com/questions/17558096/animated-title-in-matplotlib) – ImportanceOfBeingErnest Feb 15 '17 at 20:57
  • See especially [this solution](http://stackoverflow.com/a/30860027/4124317) inside [Animated title in matplotlib](http://stackoverflow.com/questions/17558096/animated-title-in-matplotlib). – ImportanceOfBeingErnest Feb 15 '17 at 20:58

1 Answers1

2

I got it working with the monkey patch suggested in this post.

Hope this works for wandering souls (like me) that spent A LOT of time trying to solve this problem.

Lino Bossio
  • 103
  • 1
  • 8