|
3 | 3 | Shared axis |
4 | 4 | =========== |
5 | 5 |
|
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 and 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 |
22 | 20 | """ |
23 | 21 | import matplotlib.pyplot as plt |
24 | 22 | import numpy as np |
25 | 23 |
|
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 | + |
46 | 38 | plt.show() |
47 | 39 |
|
48 | 40 | # %% |
|
0 commit comments