FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Backport PR #23929 on branch v3.6.0-doc (DOC: add animation example to show animated subplots) by meeseeksmachine · Pull Request #23934 · matplotlib/matplotlib · GitHub

Repository navigation

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (1) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
79 changes: 79 additions & 0 deletions examples/animation/multiple_axes.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
"""
=======================
Multiple axes animation
=======================

This example showcases:

- how animation across multiple subplots works,
- using a figure artist in the animation.
"""

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from matplotlib.patches import ConnectionPatch

fig, (axl, axr) = plt.subplots(
ncols=2,
sharey=True,
figsize=(6, 2),
gridspec_kw=dict(width_ratios=[1, 3], wspace=0),
)
axl.set_aspect(1)
axr.set_box_aspect(1 / 3)
axr.yaxis.set_visible(False)
axr.xaxis.set_ticks([0, np.pi, 2 * np.pi], ["0", r"$\pi$", r"$2\pi$"])

# draw circle with initial point in left Axes
x = np.linspace(0, 2 * np.pi, 50)
axl.plot(np.cos(x), np.sin(x), "k", lw=0.3)
point, = axl.plot(0, 0, "o")

# draw full curve to set view limits in right Axes
sine, = axr.plot(x, np.sin(x))

# draw connecting line between both graphs
con = ConnectionPatch(
(1, 0),
(0, 0),
"data",
"data",
axesA=axl,
axesB=axr,
color="C0",
ls="dotted",
)
fig.add_artist(con)


def animate(i):
pos = np.cos(i), np.sin(i)
point.set_data(*pos)
x = np.linspace(0, i, int(i * 25 / np.pi))
sine.set_data(x, np.sin(x))
con.xy1 = pos
con.xy2 = i, pos[1]
return point, sine, con


ani = animation.FuncAnimation(
fig,
animate,
interval=50,
blit=False, # blitting can't be used with Figure artists
frames=x,
repeat_delay=100,
)

plt.show()

#############################################################################
#
# .. admonition:: References
#
# The use of the following functions, methods, classes and modules is shown
# in this example:
#
# - `matplotlib.patches.ConnectionPatch`
# - `matplotlib.animation.FuncAnimation`

Back | FazBrowse Home | New Git URL