Repository navigation
Closed
Milestone
Description
Bug report
the animation doesn't work
Bug summary
Code for reproduction
import numpy as np
#import matplotlib
#matplotlib.use('Agg')
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from mpl_toolkits.axes_grid1 import host_subplot
import mpl_toolkits.axisartist as AA
host = host_subplot(111, axes_class=AA.Axes)
fig=host.get_figure()
plt.subplots_adjust(right=0.75)
par1 = host.twinx()
par2 = host.twinx()
#fig, ax = plt.subplots()
offset = 60
new_fixed_axis = par2.get_grid_helper().new_fixed_axis
par2.axis["right"] = new_fixed_axis(loc="right",
axes=par2,
offset=(offset, 0))
par1.axis["right"].toggle(all=True)
par2.axis["right"].toggle(all=True)
host.set_xlabel("Distance")
host.set_ylabel("Density")
par1.set_ylabel("Temperature")
par2.set_ylabel("Velocity")
x = np.arange(0, 2*np.pi, 0.01)
p1, = host.plot(x,np.sin(x), label="Density")
p2, = par1.plot(x, np.cos(x), label="Temperature")
p3, = par2.plot(x, np.tan(x), label="Velocity")
#line, = ax.plot(x, np.sin(x))
def init(): # only required for blitting to give a clean slate.
p1.set_ydata([np.nan] * len(x))
p2.set_ydata([np.nan] * len(x))
p3.set_ydata([np.nan] * len(x))
return p1,p2,p3
def animate(i):
#line.set_ydata(np.sin(x + i / 100)) # update the data.
p1.set_ydata(np.sin(x + i / 100))
p2.set_ydata(np.cos(x + i / 100))
p3.set_ydata(np.tan(x + i / 100))
return p1,p2,p3
ani = animation.FuncAnimation(
fig, animate, init_func=init, interval=2, blit=True, save_count=50)
# To save the animation, use e.g.
#
# ani.save("movie.mp4")
#
# or
#
# from matplotlib.animation import FFMpegWriter
# writer = FFMpegWriter(fps=15, metadata=dict(artist='Me'), bitrate=1800)
# ani.save("movie.mp4", writer=writer)
plt.show()Actual outcome
not work
Process finished with exit code 1
# If applicable, paste the console output here
#
#
Expected outcome
Matplotlib version
- Operating system:
- Matplotlib version:
- Matplotlib backend (
print(matplotlib.get_backend())): - Python version:
- Jupyter version (if applicable):
- Other libraries:
Activity
charliboy commentedon Jul 9, 2018
the animation does't work .Can anyone give me some advice ?.thanks.
jklymak commentedon Jul 9, 2018
plt.show()indeed doesn't work for meBut saving the animation works fine.
tacaswell commentedon Jul 9, 2018
Before you call
plt.showtry adding afig.canvas.draw(). I suspect there is a race condition here (likely in AxisArtist) where we try to blit something before it has been drawn once.charliboy commentedon Jul 9, 2018
@tacaswell thanks, it doesn't seem to be effective. The prompt as follows:
"AttributeError: draw_artist can only be used after an initial draw which caches the render"
tacaswell commentedon Jul 9, 2018
What backend? Can you sort out which artist is failing?
charliboy commentedon Jul 10, 2018
@tacaswell , I have tested tkagg and qt5agg,the problem is the same. I traced functionanimation, the artlist list and cache is empty.I can't find out where the problem is.
ImportanceOfBeingErnest commentedon Jul 10, 2018
I can run the code above on windows 8, python 3.6 on master as well as 2.2.2, Qt5Agg without any error. However there is no animation happening.
Running the code on python 2.7 with matplotlib 2.2.2 produces the reported error (
AttributeError: draw_artist can only be used after an initial draw which caches the renderer) for me.tacaswell commentedon Jul 10, 2018
I can reproduce with py3.8 and matplotlib master.
If you pass
blit=Falseit displays correctly.The reason saving works is that we re-draw every frame when saving (and ignore blitting).
The issue is that only the host axes really ends up in the draw
tree of the figure and in the draw method it pulls out some artists from parsite axes to draw but does not actually draw the parasite axes.
matplotlib/lib/mpl_toolkits/axes_grid1/parasite_axes.py
Lines 236 to 262 in 734c194
When we go to blit we rely on the on the renderer to be stashed on the axes object, but due to the above code the parasite axes have never been drawn (at the axes level) so they don't have that stashed and hence the exception we see.
Applying the following
fixes blitting, but I think the axes ticks are being draw multiple times.
jklymak commentedon Jul 10, 2018
We do have twinx in the root library, so maybe it’s not worth fixing this for what is an axes grid problem. OTOH I’m not sure the example above is 100% reproducible with the main library...
ImportanceOfBeingErnest commentedon Jul 10, 2018
Any idea on why the code would not produce the error on windows for me?
It seems indeed be limited to axesgrid. Using the usual twinx mechanism from the main library, the code could look like the following
Multiple axis animation code.
It works, but seems to have a different problem of not initializing blitting correctly.
Even without animation, the axesgrid solution looks like it draws the axis decorators twice.
tacaswell commentedon Jul 10, 2018
@ImportanceOfBeingErnest do other animations with blitting work for you on 3.6 on windows? Bit worried you have uncovered a more fundamental bug...
ImportanceOfBeingErnest commentedon Jul 11, 2018
Sorry, I can now reproduce the reported error here. If I run the code shown outside of Spyder, the error is raised as seen above.
I do encounter various other strange things with animations and blitting, but I currently have problems reproducing them, or distilling them into a minimal example. In any case they are unrelated to this problem of animating in
parasite_axes, so I would then open a new issue about it.charliboy commentedon Jul 13, 2018
@tacaswell ,thank you very much. I'll switch to axes.twinx() for the time being. I hope you can upgrade and fix bug. If you stop talking about , please help me close issue.
anntzer commentedon Jul 13, 2018
It seems like the easy fix is just to stash the cached renderer on the figure rather than on the axes. In fact that's already done, we just need to stop fetching it from the axes:
Now the animation runs mostly fine... except that the first draw is not cleared and stays drawn (but that's hopefully not too hard to fix).
jklymak commentedon Feb 11, 2019
This works on master, so I think the above PRs fixed?
Add command line option to calibration-viewer.py to control blitting.