Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

64 changes: 28 additions & 36 deletions galleries/examples/subplots_axes_and_figures/shared_axis_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,38 @@
Shared axis
===========

You can share the x- or y-axis limits for one axis with another by
passing an `~.axes.Axes` instance as a *sharex* or *sharey* keyword argument.

Changing the axis limits on one Axes will be reflected automatically
in the other, and vice-versa, so when you navigate with the toolbar
the Axes will follow each other on their shared axis. Ditto for
changes in the axis scaling (e.g., log vs. linear). However, it is
possible to have differences in tick labeling, e.g., you can selectively
turn off the tick labels on one Axes.

The example below shows how to customize the tick labels on the
various axes. Shared axes share the tick locator, tick formatter,
view limits, and transformation (e.g., log, linear). But the tick labels
themselves do not share properties. This is a feature and not a bug,
because you may want to make the tick labels smaller on the upper
axes, e.g., in the example below.
Use axis sharing when you want to compare data across multiple subplots, and want to
ensure they are on the same scale. To do so, pass ``sharex=True`` and/or ``sharey=True``
to `~.pyplot.subplots`.

This ensures the x- or y-axis limits are synchronized across the subplots. Autoscaling
considers the data on all Axes; therefore, any limit changes, including interactive zoom
and pan, will affect all shared axes.

The plot below illustrates this by showing two different time-series and using *sharex*
to ensure the times are aligned.

For more info see :ref:`sharing-axes`.

.. redirect-from:: /gallery/subplots_axes_and_figures/share_axis_lims_views
"""
import matplotlib.pyplot as plt
import numpy as np

t = np.arange(0.01, 5.0, 0.01)
s1 = np.sin(2 * np.pi * t)
s2 = np.exp(-t)
s3 = np.sin(4 * np.pi * t)

ax1 = plt.subplot(311)
plt.plot(t, s1)
# reduce the fontsize of the tick labels
plt.tick_params('x', labelsize=6)

# share x only
ax2 = plt.subplot(312, sharex=ax1)
plt.plot(t, s2)
# make these tick labels invisible
plt.tick_params('x', labelbottom=False)

# share x and y
ax3 = plt.subplot(313, sharex=ax1, sharey=ax1)
plt.plot(t, s3)
plt.xlim(0.01, 5.0)
t1 = np.linspace(0, 8, 201)
y1 = np.sin(2 * np.pi * t1)
t2 = np.linspace(2, 10, 201)
y2 = 20 * np.cos(2 * np.pi * t2)**2 * np.exp(-0.3*t2)

fig, (ax1, ax2) = plt.subplots(2, sharex=True)

ax1.plot(t1, y1)
ax1.set_ylabel("Signal 1")

ax2.plot(t2, y2)
ax2.set_ylabel("Signal 2")
ax2.set_xlabel("Time (s)")

plt.show()

# %%
Expand Down
2 changes: 2 additions & 0 deletions galleries/examples/subplots_axes_and_figures/subplots_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@
ax.label_outer()

# %%
# .. _sharing-axes:
#
# Sharing axes
# """"""""""""
#
Expand Down