Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e729962

Browse files
committed
DOC: Update tutorial to explain colorbar API
1 parent 3d64d65 commit e729962

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

lib/matplotlib/axes/_subplots.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,9 @@ def _make_twin_axes(self, *kl, **kwargs):
173173
"""
174174
from matplotlib.projections import process_projection_requirements
175175
if 'sharex' in kwargs and 'sharey' in kwargs:
176-
raise ValueError("Twinned Axes may share only one axis.")
176+
if (not (kwargs['sharex'] == self ) and
177+
not (kwargs['sharex'] == self)):
178+
raise ValueError("Twinned Axes may share only one axis.")
177179
kl = (self.get_subplotspec(),) + kl
178180
projection_class, kwargs, key = process_projection_requirements(
179181
self.figure, *kl, **kwargs)

tutorials/intermediate/constrainedlayout_guide.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ def example_plot(ax, fontsize=12, nodec=False):
105105
fig.colorbar(im, ax=ax, shrink=0.6)
106106

107107
############################################################################
108-
# If you specify multiple axes to the ``ax`` argument of ``colorbar``,
108+
# If you specify a list of axes to the ``ax`` argument of ``colorbar``,
109109
# constrained_layout will take space from all axes that share the same
110110
# gridspec.
111111

@@ -114,6 +114,31 @@ def example_plot(ax, fontsize=12, nodec=False):
114114
im = ax.pcolormesh(arr, rasterized=True)
115115
fig.colorbar(im, ax=axs, shrink=0.6)
116116

117+
############################################################################
118+
# Note that there is a bit of a subtlety when specifying a single axes
119+
# as the parent. In the following, it might be desirable and expected
120+
# for the colorbars to line up, but they don't because the colorbar paired
121+
# with the bottom axes is tied to the subplotspec of the axes, and hence
122+
# shrinks when the gridspec-level colorbar is added.
123+
124+
fig, axs = plt.subplots(3, 1, figsize=(4, 4), constrained_layout=True)
125+
for ax in axs[:2]:
126+
im = ax.pcolormesh(arr, rasterized=True)
127+
fig.colorbar(im, ax=axs[:2], shrink=0.6)
128+
im = axs[2].pcolormesh(arr, rasterized=True)
129+
fig.colorbar(im, ax=axs[2], shrink=0.6)
130+
131+
############################################################################
132+
# The API to make a single-axes behave like a list of axes is to specify
133+
# it as a list, as below:
134+
135+
fig, axs = plt.subplots(3, 1, figsize=(4, 4), constrained_layout=True)
136+
for ax in axs[:2]:
137+
im = ax.pcolormesh(arr, rasterized=True)
138+
fig.colorbar(im, ax=axs[:2], shrink=0.6)
139+
im = axs[2].pcolormesh(arr, rasterized=True)
140+
fig.colorbar(im, ax=[axs[2]], shrink=0.6)
141+
117142
####################################################
118143
# Suptitle
119144
# =========

0 commit comments

Comments
 (0)