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

Skip to content

Commit 6df7706

Browse files
committed
DOC: cleanup constrained layout tutorial
1 parent 78442c4 commit 6df7706

File tree

2 files changed

+48
-38
lines changed

2 files changed

+48
-38
lines changed

examples/subplots_axes_and_figures/colorbar_placement.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
"""
2+
.. _colorbar_placement:
3+
24
=================
35
Placing Colorbars
46
=================

tutorials/intermediate/constrainedlayout_guide.py

Lines changed: 46 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,46 @@
33
Constrained Layout Guide
44
================================
55
6-
How to use constrained-layout to fit plots within your figure cleanly.
6+
Use *constrained layout* to fit plots within your figure cleanly.
77
8-
*constrained_layout* automatically adjusts subplots and decorations like
9-
legends and colorbars so that they fit in the figure window while still
10-
preserving, as best they can, the logical layout requested by the user.
8+
*Constrained layout* automatically adjusts subplots so that decorations like tick
9+
labels, legends, and colorbars do not overlap, while still preserving the
10+
logical layout requested by the user.
1111
12-
*constrained_layout* is similar to
13-
:doc:`tight_layout</tutorials/intermediate/tight_layout_guide>`,
14-
but uses a constraint solver to determine the size of axes that allows
15-
them to fit.
12+
*Constrained layout* is similar to :doc:`Tight
13+
layout</tutorials/intermediate/tight_layout_guide>`, but is substantially more
14+
flexible. It handles colorbars placed on multiple axes
15+
(:ref:`colorbar_placement`) nested layouts (`~.Figure.subfigures`) and Axes that
16+
span rows or columns (`~.pyplot.subplot_mosaic`), striving to align spines from
17+
axes in the same row or column. In addition, :ref:`Compressed layout
18+
<compressed_layout>` will try and move fixed aspect-ratio axes closer together.
19+
These features are described in this document, as well as some
20+
:ref:`implementation details <cl_notes_on_algorithm>` discussed at the end.
1621
17-
*constrained_layout* typically needs to be activated before any axes are
18-
added to a figure. Two ways of doing so are
22+
*Constrained layout* typically needs to be activated before any axes are added to
23+
a figure. Two ways of doing so are
1924
2025
* using the respective argument to :func:`~.pyplot.subplots` or
2126
:func:`~.pyplot.figure`, e.g.::
2227
2328
plt.subplots(layout="constrained")
2429
25-
* activate it via :ref:`rcParams<customizing-with-dynamic-rc-settings>`,
26-
like::
30+
* activate it via :ref:`rcParams<customizing-with-dynamic-rc-settings>`, like::
2731
2832
plt.rcParams['figure.constrained_layout.use'] = True
2933
3034
Those are described in detail throughout the following sections.
3135
32-
Simple Example
36+
.. warning::
37+
38+
Calling ``plt.tight_layout()`` will turn off *constrained layout*!
39+
40+
Simple example
3341
==============
3442
3543
In Matplotlib, the location of axes (including subplots) are specified in
36-
normalized figure coordinates. It can happen that your axis labels or
37-
titles (or sometimes even ticklabels) go outside the figure area, and are thus
44+
normalized figure coordinates. It can happen that your axis labels or titles
45+
(or sometimes even ticklabels) go outside the figure area, and are thus
3846
clipped.
3947
"""
4048

@@ -93,21 +101,19 @@ def example_plot(ax, fontsize=12, hide_labels=False):
93101
example_plot(ax)
94102

95103
# %%
104+
#
96105
# Colorbars
97106
# =========
98107
#
99-
# If you create a colorbar with `.Figure.colorbar`,
100-
# you need to make room for it. ``constrained_layout`` does this
101-
# automatically. Note that if you specify ``use_gridspec=True`` it will be
102-
# ignored because this option is made for improving the layout via
103-
# ``tight_layout``.
108+
# If you create a colorbar with `.Figure.colorbar`, you need to make room for
109+
# it. *Constrained layout* does this automatically. Note that if you
110+
# specify ``use_gridspec=True`` it will be ignored because this option is made
111+
# for improving the layout via ``tight_layout``.
104112
#
105113
# .. note::
106114
#
107115
# For the `~.axes.Axes.pcolormesh` keyword arguments (``pc_kwargs``) we use a
108-
# dictionary. Below we will assign one colorbar to a number of axes each
109-
# containing a `~.cm.ScalarMappable`; specifying the norm and colormap
110-
# ensures the colorbar is accurate for all the axes.
116+
# dictionary to keep the calls consistent across this document.
111117

112118
arr = np.arange(100).reshape((10, 10))
113119
norm = mcolors.Normalize(vmin=0., vmax=100.)
@@ -142,7 +148,7 @@ def example_plot(ax, fontsize=12, hide_labels=False):
142148
# Suptitle
143149
# =========
144150
#
145-
# ``constrained_layout`` can also make room for `~.Figure.suptitle`.
151+
# *Constrained layout* can also make room for `~.Figure.suptitle`.
146152

147153
fig, axs = plt.subplots(2, 2, figsize=(4, 4), layout="constrained")
148154
for ax in axs.flat:
@@ -234,7 +240,7 @@ def example_plot(ax, fontsize=12, hide_labels=False):
234240
#
235241

236242
# %%
237-
# Padding and Spacing
243+
# Padding and spacing
238244
# ===================
239245
#
240246
# Padding between axes is controlled in the horizontal by *w_pad* and
@@ -274,7 +280,7 @@ def example_plot(ax, fontsize=12, hide_labels=False):
274280

275281
# %%
276282
# GridSpecs also have optional *hspace* and *wspace* keyword arguments,
277-
# that will be used instead of the pads set by ``constrained_layout``:
283+
# that will be used instead of the pads set by *constrained layout*:
278284

279285
fig, axs = plt.subplots(2, 2, layout="constrained",
280286
gridspec_kw={'wspace': 0.3, 'hspace': 0.2})
@@ -424,7 +430,7 @@ def example_plot(ax, fontsize=12, hide_labels=False):
424430

425431
# %%
426432
# Rather than using subgridspecs, Matplotlib now provides `~.Figure.subfigures`
427-
# which also work with ``constrained_layout``:
433+
# which also work with *constrained layout*:
428434

429435
fig = plt.figure(layout="constrained")
430436
sfigs = fig.subfigures(1, 2, width_ratios=[1, 2])
@@ -448,7 +454,7 @@ def example_plot(ax, fontsize=12, hide_labels=False):
448454
#
449455
# There can be good reasons to manually set an Axes position. A manual call
450456
# to `~.axes.Axes.set_position` will set the axes so constrained_layout has
451-
# no effect on it anymore. (Note that ``constrained_layout`` still leaves the
457+
# no effect on it anymore. (Note that *constrained layout* still leaves the
452458
# space for the axes that is moved).
453459

454460
fig, axs = plt.subplots(1, 2, layout="constrained")
@@ -461,7 +467,7 @@ def example_plot(ax, fontsize=12, hide_labels=False):
461467
# Grids of fixed aspect-ratio Axes: "compressed" layout
462468
# =====================================================
463469
#
464-
# ``constrained_layout`` operates on the grid of "original" positions for
470+
# *Constrained layout* operates on the grid of "original" positions for
465471
# axes. However, when Axes have fixed aspect ratios, one side is usually made
466472
# shorter, and leaves large gaps in the shortened direction. In the following,
467473
# the Axes are square, but the figure quite wide so there is a horizontal gap:
@@ -485,17 +491,17 @@ def example_plot(ax, fontsize=12, hide_labels=False):
485491

486492

487493
# %%
488-
# Manually turning off ``constrained_layout``
494+
# Manually turning off *constrained layout*
489495
# ===========================================
490496
#
491-
# ``constrained_layout`` usually adjusts the axes positions on each draw
497+
# *Constrained layout* usually adjusts the axes positions on each draw
492498
# of the figure. If you want to get the spacing provided by
493-
# ``constrained_layout`` but not have it update, then do the initial
499+
# *Constrained layout* but not have it update, then do the initial
494500
# draw and then call ``fig.set_layout_engine(None)``.
495501
# This is potentially useful for animations where the tick labels may
496502
# change length.
497503
#
498-
# Note that ``constrained_layout`` is turned off for ``ZOOM`` and ``PAN``
504+
# Note that *Constrained layout* is turned off for ``ZOOM`` and ``PAN``
499505
# GUI events for the backends that use the toolbar. This prevents the
500506
# axes from changing position during zooming and panning.
501507
#
@@ -506,11 +512,11 @@ def example_plot(ax, fontsize=12, hide_labels=False):
506512
# Incompatible functions
507513
# ----------------------
508514
#
509-
# ``constrained_layout`` will work with `.pyplot.subplot`, but only if the
515+
# *Constrained layout* will work with `.pyplot.subplot`, but only if the
510516
# number of rows and columns is the same for each call.
511517
# The reason is that each call to `.pyplot.subplot` will create a new
512518
# `.GridSpec` instance if the geometry is not the same, and
513-
# ``constrained_layout``. So the following works fine:
519+
# *Constrained layout*. So the following works fine:
514520

515521
fig = plt.figure(layout="constrained")
516522

@@ -557,10 +563,10 @@ def example_plot(ax, fontsize=12, hide_labels=False):
557563
fig.suptitle('subplot2grid')
558564

559565
# %%
560-
# Other Caveats
566+
# Other caveats
561567
# -------------
562568
#
563-
# * ``constrained_layout`` only considers ticklabels, axis labels, titles, and
569+
# * *Constrained layout* only considers ticklabels, axis labels, titles, and
564570
# legends. Thus, other artists may be clipped and also may overlap.
565571
#
566572
# * It assumes that the extra space needed for ticklabels, axis labels,
@@ -595,6 +601,8 @@ def example_plot(ax, fontsize=12, hide_labels=False):
595601
# not require outside data or dependencies (other than numpy).
596602

597603
# %%
604+
# .. _cl_notes_on_algorithm:
605+
#
598606
# Notes on the algorithm
599607
# ======================
600608
#
@@ -691,7 +699,7 @@ def example_plot(ax, fontsize=12, hide_labels=False):
691699
# is a conscious decision of the algorithm, and leads to the case where
692700
# the two right-hand axes have the same height, but it is not 1/2 the height
693701
# of the left-hand axes. This is consistent with how ``gridspec`` works
694-
# without constrained layout.
702+
# without *constrained layout*.
695703

696704
fig = plt.figure(layout="constrained")
697705
gs = gridspec.GridSpec(2, 2, figure=fig)

0 commit comments

Comments
 (0)