0

I was trying to make an animation using pcolormesh but my code was having problems, so I decided to use one of the answers in this post as a starting point. The answer had too many plots in it and I only needed the pcolormesh, so I removed the unnecessary plots. The puzzling thing is that removing the plots broke the animation for some reason. I made some minimal example that contains the pcolormesh and some text. The code works as long as I don't comment the text update but I'm unable to figure why.

My questions are the following: Is there any reason the pcolormesh won't update in some scenarios? How can I fix the animation without adding other plots or text?

Note: I'm using OSX which apparenlty has some issues with the blit option.

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

y, x = np.meshgrid(np.linspace(-10, 10,100), np.linspace(-10, 10,100))
z = np.sin(x)*np.sin(x)+np.sin(y)*np.sin(y)

fig = plt.figure(figsize=(16, 8),facecolor='white')
ax = plt.subplot()
quad = ax.pcolormesh(x,y,z,shading='gouraud')
ax.set_xlabel('time')
ax.set_ylabel('amplitude')
cb = fig.colorbar(quad,ax=ax)
time_text = ax.text(0.02, 0.95, 'start', transform=ax.transAxes)

def animate(iter):
    z = np.sin(x-iter/(2*np.pi))*np.sin(x-iter/(2*np.pi))+np.sin(y)*np.sin(y)
    time_text.set_text(str(iter)) #comment this line to break the animation
    quad.set_array(z.ravel())

anim = animation.FuncAnimation(fig,animate,frames=100,interval=50,blit=False,repeat=False)
plt.show()

Edit: As it was pointed out in the comments, it had to do with the backend. My updated questions are: why does this code fail for some backends? Shouldn't the behavior of pcolormesh be independent of the text added?

German Capuano
  • 5,183
  • 6
  • 23
  • 35
  • Possible duplicate of [How to animate the colorbar in matplotlib](https://stackoverflow.com/questions/39472017/how-to-animate-the-colorbar-in-matplotlib) – Sheldore Oct 02 '18 at 15:01
  • 1
    The animation works for me on both Ubuntu and MacOS, even with the text update commented out. Maybe you have something weird on your system? – adamconkey Oct 02 '18 at 15:07
  • @adamconkey: The animation works for me too but I guess the OP wants the **colorbar** to be updated. I am not sure though that's why I suggested a duplicate to animate the colorbar as well – Sheldore Oct 02 '18 at 15:14
  • @Bazingaa Maybe there's something weird in my system, but I'm using anaconda3. And no, I'm not talking about the colorbar. It just doesn't update at all and I don't think it's duplicate. – German Capuano Oct 02 '18 at 16:01
  • 1
    Ok, vote retracted – Sheldore Oct 02 '18 at 16:26
  • 1
    This is hardly reproducible. You may look into which backend you are using and potentially change it to e.g. `"TkAgg"` or `"Qt5Agg"`. – ImportanceOfBeingErnest Oct 02 '18 at 16:43
  • @ImportanceOfBeingErnest Yes, you were right. Changing the backend fixed it, but it's weird. Why doesn't it work in the default backend? seems very buggy to me. – German Capuano Oct 02 '18 at 18:45
  • 1
    It might be that there is a bug in the mac os backend. Since I don't use Macs, I cannot test, nor give any hint on possible reasons. It might also be related to gouraud-shading. If you already use the newest matplotlib version (either 2.2.3 or 3.0.0) and can reproduce this problem reliably feel free to open an issue at the [github issue tracker](https://github.com/matplotlib/matplotlib/issues). – ImportanceOfBeingErnest Oct 02 '18 at 20:21

0 Answers0