|
| 1 | +""" |
| 2 | +======================= |
| 3 | +Multiple axes animation |
| 4 | +======================= |
| 5 | +
|
| 6 | +This example showcases: |
| 7 | +
|
| 8 | +- how animation across multiple subplots works, |
| 9 | +- using a figure artist in the animation. |
| 10 | +""" |
| 11 | + |
| 12 | +importnumpyasnp |
| 13 | +importmatplotlib.pyplotasplt |
| 14 | +importmatplotlib.animationasanimation |
| 15 | +frommatplotlib.patchesimportConnectionPatch |
| 16 | + |
| 17 | +fig, (axl,axr)=plt.subplots( |
| 18 | +ncols=2, |
| 19 | +sharey=True, |
| 20 | +figsize=(6,2), |
| 21 | +gridspec_kw=dict(width_ratios=[1,3],wspace=0), |
| 22 | +) |
| 23 | +axl.set_aspect(1) |
| 24 | +axr.set_box_aspect(1/3) |
| 25 | +axr.yaxis.set_visible(False) |
| 26 | +axr.xaxis.set_ticks([0,np.pi,2*np.pi], ["0",r"$\pi$",r"$2\pi$"]) |
| 27 | + |
| 28 | +# draw circle with initial point in left Axes |
| 29 | +x=np.linspace(0,2*np.pi,50) |
| 30 | +axl.plot(np.cos(x),np.sin(x),"k",lw=0.3) |
| 31 | +point,=axl.plot(0,0,"o") |
| 32 | + |
| 33 | +# draw full curve to set view limits in right Axes |
| 34 | +sine,=axr.plot(x,np.sin(x)) |
| 35 | + |
| 36 | +# draw connecting line between both graphs |
| 37 | +con=ConnectionPatch( |
| 38 | + (1,0), |
| 39 | + (0,0), |
| 40 | +"data", |
| 41 | +"data", |
| 42 | +axesA=axl, |
| 43 | +axesB=axr, |
| 44 | +color="C0", |
| 45 | +ls="dotted", |
| 46 | +) |
| 47 | +fig.add_artist(con) |
| 48 | + |
| 49 | + |
| 50 | +defanimate(i): |
| 51 | +pos=np.cos(i),np.sin(i) |
| 52 | +point.set_data(*pos) |
| 53 | +x=np.linspace(0,i,int(i*25/np.pi)) |
| 54 | +sine.set_data(x,np.sin(x)) |
| 55 | +con.xy1=pos |
| 56 | +con.xy2=i,pos[1] |
| 57 | +returnpoint,sine,con |
| 58 | + |
| 59 | + |
| 60 | +ani=animation.FuncAnimation( |
| 61 | +fig, |
| 62 | +animate, |
| 63 | +interval=50, |
| 64 | +blit=False,# blitting can't be used with Figure artists |
| 65 | +frames=x, |
| 66 | +repeat_delay=100, |
| 67 | +) |
| 68 | + |
| 69 | +plt.show() |
| 70 | + |
| 71 | +############################################################################# |
| 72 | +# |
| 73 | +# .. admonition:: References |
| 74 | +# |
| 75 | +# The use of the following functions, methods, classes and modules is shown |
| 76 | +# in this example: |
| 77 | +# |
| 78 | +# - `matplotlib.patches.ConnectionPatch` |
| 79 | +# - `matplotlib.animation.FuncAnimation` |