Skip to content

Commit 60ccd84

Browse files
authored
Merge pull request matplotlib#31851 from meeseeksmachine/auto-backport-of-pr-31605-on-v3.11.x
Backport PR matplotlib#31605 on branch v3.11.x (DOC: Consolidate shared axis examples)
2 parents 2129fb1 + 09c1586 commit 60ccd84

3 files changed

Lines changed: 30 additions & 68 deletions

File tree

galleries/examples/subplots_axes_and_figures/share_axis_lims_views.py

Lines changed: 0 additions & 32 deletions
This file was deleted.

galleries/examples/subplots_axes_and_figures/shared_axis_demo.py

Lines changed: 28 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,46 +3,38 @@
33
Shared axis
44
===========
55
6-
You can share the x- or y-axis limits for one axis with another by
7-
passing an `~.axes.Axes` instance as a *sharex* or *sharey* keyword argument.
8-
9-
Changing the axis limits on one Axes will be reflected automatically
10-
in the other, and vice-versa, so when you navigate with the toolbar
11-
the Axes will follow each other on their shared axis. Ditto for
12-
changes in the axis scaling (e.g., log vs. linear). However, it is
13-
possible to have differences in tick labeling, e.g., you can selectively
14-
turn off the tick labels on one Axes.
15-
16-
The example below shows how to customize the tick labels on the
17-
various axes. Shared axes share the tick locator, tick formatter,
18-
view limits, and transformation (e.g., log, linear). But the tick labels
19-
themselves do not share properties. This is a feature and not a bug,
20-
because you may want to make the tick labels smaller on the upper
21-
axes, e.g., in the example below.
6+
Use axis sharing when you want to compare data across multiple subplots, and want to
7+
ensure they are on the same scale. To do so, pass ``sharex=True`` and/or ``sharey=True``
8+
to `~.pyplot.subplots`.
9+
10+
This ensures the x- or y-axis limits are synchronized across the subplots. Autoscaling
11+
considers the data on all Axes; therefore, any limit changes, including interactive zoom
12+
and pan, will affect all shared axes.
13+
14+
The plot below illustrates this by showing two different time-series and using *sharex*
15+
to ensure the times are aligned.
16+
17+
For more info see :ref:`sharing-axes`.
18+
19+
.. redirect-from:: /gallery/subplots_axes_and_figures/share_axis_lims_views
2220
"""
2321
import matplotlib.pyplot as plt
2422
import numpy as np
2523

26-
t = np.arange(0.01, 5.0, 0.01)
27-
s1 = np.sin(2 * np.pi * t)
28-
s2 = np.exp(-t)
29-
s3 = np.sin(4 * np.pi * t)
30-
31-
ax1 = plt.subplot(311)
32-
plt.plot(t, s1)
33-
# reduce the fontsize of the tick labels
34-
plt.tick_params('x', labelsize=6)
35-
36-
# share x only
37-
ax2 = plt.subplot(312, sharex=ax1)
38-
plt.plot(t, s2)
39-
# make these tick labels invisible
40-
plt.tick_params('x', labelbottom=False)
41-
42-
# share x and y
43-
ax3 = plt.subplot(313, sharex=ax1, sharey=ax1)
44-
plt.plot(t, s3)
45-
plt.xlim(0.01, 5.0)
24+
t1 = np.linspace(0, 8, 201)
25+
y1 = np.sin(2 * np.pi * t1)
26+
t2 = np.linspace(2, 10, 201)
27+
y2 = 20 * np.cos(2 * np.pi * t2)**2 * np.exp(-0.3*t2)
28+
29+
fig, (ax1, ax2) = plt.subplots(2, sharex=True)
30+
31+
ax1.plot(t1, y1)
32+
ax1.set_ylabel("Signal 1")
33+
34+
ax2.plot(t2, y2)
35+
ax2.set_ylabel("Signal 2")
36+
ax2.set_xlabel("Time (s)")
37+
4638
plt.show()
4739

4840
# %%

galleries/examples/subplots_axes_and_figures/subplots_demo.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@
108108
ax.label_outer()
109109

110110
# %%
111+
# .. _sharing-axes:
112+
#
111113
# Sharing axes
112114
# """"""""""""
113115
#

0 commit comments

Comments
 (0)