| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 6665bc7 commit 553bfab
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -9,8 +9,24 @@ | |||
| 9 | 9 | legends and colorbars so that they fit in the figure window while still | |
| 10 | 10 | preserving, as best they can, the logical layout requested by the user. | |
| 11 | 11 | ||
| 12 | - *constrained_layout* is similar to *tight_layout*, but uses a constraint | ||
| 13 | - solver to determine the size of axes that allows them to fit. | ||
| 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. | ||
| 16 | + | ||
| 17 | + *constrained_layout* needs to be activated before any axes are added to | ||
| 18 | + a figure. Two ways of doing so are | ||
| 19 | + | ||
| 20 | + * using the respective argument to :func:`~.pyplot.subplots` or | ||
| 21 | + :func:`~.pyplot.figure`, e.g.:: | ||
| 22 | + | ||
| 23 | + plt.subplots(contrained_layout=True) | ||
| 24 | + | ||
| 25 | + * activate it via :ref:`rcParams<matplotlib-rcparams>`, like:: | ||
| 26 | + | ||
| 27 | + plt.rcParams['figure.constrained_layout.use'] = True | ||
| 28 | + | ||
| 29 | + Those are described in detail throughout the following sections. | ||
| 14 | 30 | ||
| 15 | 31 | .. warning:: | |
| 16 | 32 | ||
@@ -83,7 +99,7 @@ def example_plot(ax, fontsize=12, nodec=False): | |||
| 83 | 99 | example_plot(ax) | |
| 84 | 100 | ||
| 85 | 101 | ############################################################################### | |
| 86 | - # Specifying `constrained_layout=True` in the call to `plt.subplots` | ||
| 102 | + # Specifying ``constrained_layout=True`` in the call to ``plt.subplots`` | ||
| 87 | 103 | # causes the layout to be properly constrained. | |
| 88 | 104 | ||
| 89 | 105 | fig, axs = plt.subplots(2, 2, constrained_layout=True) | |
@@ -102,9 +118,9 @@ def example_plot(ax, fontsize=12, nodec=False): | |||
| 102 | 118 | # | |
| 103 | 119 | # .. note:: | |
| 104 | 120 | # | |
| 105 | - # For the `pcolormesh` kwargs (``pc_kwargs``) we use a dictionary. | ||
| 121 | + # For the `~.axes.Axes.pcolormesh` kwargs (``pc_kwargs``) we use a dictionary. | ||
| 106 | 122 | # Below we will assign one colorbar to a number of axes each containing | |
| 107 | - # a `ScalarMappable`; specifying the norm and colormap ensures | ||
| 123 | + # a `~.cm.ScalarMappable`; specifying the norm and colormap ensures | ||
| 108 | 124 | # the colorbar is accurate for all the axes. | |
| 109 | 125 | ||
| 110 | 126 | arr = np.arange(100).reshape((10, 10)) | |
@@ -117,7 +133,8 @@ def example_plot(ax, fontsize=12, nodec=False): | |||
| 117 | 133 | ||
| 118 | 134 | ############################################################################ | |
| 119 | 135 | # If you specify a list of axes (or other iterable container) to the | |
| 120 | - # ``ax`` argument of ``colorbar``, constrained_layout will take space from all # axes that share the same gridspec. | ||
| 136 | + # ``ax`` argument of ``colorbar``, constrained_layout will take space from all | ||
| 137 | + # axes that share the same gridspec. | ||
| 121 | 138 | ||
| 122 | 139 | fig, axs = plt.subplots(2, 2, figsize=(4, 4), constrained_layout=True) | |
| 123 | 140 | for ax in axs.flatten(): | |
@@ -153,7 +170,7 @@ def example_plot(ax, fontsize=12, nodec=False): | |||
| 153 | 170 | # Suptitle | |
| 154 | 171 | # ========= | |
| 155 | 172 | # | |
| 156 | - # ``constrained_layout`` can also make room for ``suptitle``. | ||
| 173 | + # ``constrained_layout`` can also make room for `~.figure.Figure.suptitle`. | ||
| 157 | 174 | ||
| 158 | 175 | fig, axs = plt.subplots(2, 2, figsize=(4, 4), constrained_layout=True) | |
| 159 | 176 | for ax in axs.flatten(): | |
@@ -165,10 +182,10 @@ def example_plot(ax, fontsize=12, nodec=False): | |||
| 165 | 182 | # Legends | |
| 166 | 183 | # ======= | |
| 167 | 184 | # | |
| 168 | - # Legends can be placed outside | ||
| 169 | - # of their parent axis. Constrained-layout is designed to handle this. | ||
| 185 | + # Legends can be placed outside of their parent axis. | ||
| 186 | + # Constrained-layout is designed to handle this for :meth:`.Axes.legend`. | ||
| 170 | 187 | # However, constrained-layout does *not* handle legends being created via | |
| 171 | - # ``fig.legend()`` (yet). | ||
| 188 | + # :meth:`.Figure.legend` (yet). | ||
| 172 | 189 | ||
| 173 | 190 | fig, ax = plt.subplots(constrained_layout=True) | |
| 174 | 191 | ax.plot(np.arange(10), label='This is a plot') | |
@@ -190,8 +207,8 @@ def example_plot(ax, fontsize=12, nodec=False): | |||
| 190 | 207 | # For constrained_layout, we have implemented a padding around the edge of | |
| 191 | 208 | # each axes. This padding sets the distance from the edge of the plot, | |
| 192 | 209 | # and the minimum distance between adjacent plots. It is specified in | |
| 193 | - # inches by the keyword arguments `w_pad` and `h_pad` to the function | ||
| 194 | - # `fig.set_constrained_layout_pads`: | ||
| 210 | + # inches by the keyword arguments ``w_pad`` and ``h_pad`` to the function | ||
| 211 | + # `~.figure.Figure.set_constrained_layout_pads`: | ||
| 195 | 212 | ||
| 196 | 213 | fig, axs = plt.subplots(2, 2, constrained_layout=True) | |
| 197 | 214 | for ax in axs.flatten(): | |
@@ -210,7 +227,7 @@ def example_plot(ax, fontsize=12, nodec=False): | |||
| 210 | 227 | hspace=0., wspace=0.) | |
| 211 | 228 | ||
| 212 | 229 | ########################################## | |
| 213 | - # Spacing between subplots is set by `wspace` and `hspace`. There are | ||
| 230 | + # Spacing between subplots is set by ``wspace`` and ``hspace``. There are | ||
| 214 | 231 | # specified as a fraction of the size of the subplot group as a whole. | |
| 215 | 232 | # If the size of the figure is changed, then these spaces change in | |
| 216 | 233 | # proportion. Note in the blow how the space at the edges doesn't change from | |
@@ -229,10 +246,10 @@ def example_plot(ax, fontsize=12, nodec=False): | |||
| 229 | 246 | # Spacing with colorbars | |
| 230 | 247 | # ----------------------- | |
| 231 | 248 | # | |
| 232 | - # Colorbars still respect the `w_pad` and `h_pad` values. However they will | ||
| 233 | - # be `wspace` and `hsapce` apart from other subplots. Note the use of a `pad` | ||
| 234 | - # kwarg here in the `colorbar` call. It defaults to 0.02 of the size of the | ||
| 235 | - # axis it is attached to. | ||
| 249 | + # Colorbars still respect the ``w_pad`` and ``h_pad`` values. However they will | ||
| 250 | + # be ``wspace`` and ``hsapce`` apart from other subplots. Note the use of a | ||
| 251 | + # ``pad`` kwarg here in the ``colorbar`` call. It defaults to 0.02 of the size | ||
| 252 | + # of the axis it is attached to. | ||
| 236 | 253 | ||
| 237 | 254 | fig, axs = plt.subplots(2, 2, constrained_layout=True) | |
| 238 | 255 | for ax in axs.flatten(): | |
@@ -246,7 +263,7 @@ def example_plot(ax, fontsize=12, nodec=False): | |||
| 246 | 263 | ########################################## | |
| 247 | 264 | # In the above example, the colorbar will not ever be closer than 2 pts to | |
| 248 | 265 | # the plot, but if we want it a bit further away, we can specify its value | |
| 249 | - # for `pad` to be non-zero. | ||
| 266 | + # for ``pad`` to be non-zero. | ||
| 250 | 267 | ||
| 251 | 268 | fig, axs = plt.subplots(2, 2, constrained_layout=True) | |
| 252 | 269 | for ax in axs.flatten(): | |
@@ -259,16 +276,16 @@ def example_plot(ax, fontsize=12, nodec=False): | |||
| 259 | 276 | ||
| 260 | 277 | ########################################## | |
| 261 | 278 | # rcParams | |
| 262 | - # -------- | ||
| 279 | + # ======== | ||
| 263 | 280 | # | |
| 264 | - # There are five `rcParams` that can be set, either in a script | ||
| 265 | - # or in the `matplotlibrc` file. They all have the prefix | ||
| 266 | - # `figure.constrained_layout`: | ||
| 281 | + # There are five :ref:`rcParams<matplotlib-rcparams>` that can be set, | ||
| 282 | + # either in a script or in the `matplotlibrc` file. | ||
| 283 | + # They all have the prefix ``figure.constrained_layout``: | ||
| 267 | 284 | # | |
| 268 | - # - `use`: Whether to use constrained_layout. Default is False | ||
| 269 | - # - `w_pad`, `h_pad` Padding around axes objects. | ||
| 285 | + # - ``use``: Whether to use constrained_layout. Default is False | ||
| 286 | + # - ``w_pad``, ``h_pad``: Padding around axes objects. | ||
| 270 | 287 | # Float representing inches. Default is 3./72. inches (3 pts) | |
| 271 | - # - `wspace`, `hspace` Space between subplot groups. | ||
| 288 | + # - ``wspace``, ``hspace``: Space between subplot groups. | ||
| 272 | 289 | # Float representing a fraction of the subplot widths being separated. | |
| 273 | 290 | # Default is 0.02. | |
| 274 | 291 | ||
@@ -296,7 +313,7 @@ def example_plot(ax, fontsize=12, nodec=False): | |||
| 296 | 313 | example_plot(ax2) | |
| 297 | 314 | ||
| 298 | 315 | ############################################################################### | |
| 299 | - # More complicated gridspec layouts are possible... | ||
| 316 | + # More complicated gridspec layouts are possible. | ||
| 300 | 317 | ||
| 301 | 318 | fig = plt.figure(constrained_layout=True) | |
| 302 | 319 | ||
@@ -377,17 +394,17 @@ def docomplicated(suptitle=None): | |||
| 377 | 394 | # ================================ | |
| 378 | 395 | # | |
| 379 | 396 | # There can be good reasons to manually set an axes position. A manual call | |
| 380 | - # to `ax.set_position()` will set the axes so constrained_layout has no | ||
| 381 | - # effect on it anymore. (Note that constrained_layout still leaves the space | ||
| 382 | - # for the axes that is moved). | ||
| 397 | + # to `~.axes.Axes.set_position` will set the axes so constrained_layout has | ||
| 398 | + # no effect on it anymore. (Note that constrained_layout still leaves the | ||
| 399 | + # space for the axes that is moved). | ||
| 383 | 400 | ||
| 384 | 401 | fig, axs = plt.subplots(1, 2, constrained_layout=True) | |
| 385 | 402 | example_plot(axs[0], fontsize=12) | |
| 386 | 403 | axs[1].set_position([0.2, 0.2, 0.4, 0.4]) | |
| 387 | 404 | ||
| 388 | 405 | ############################################################################### | |
| 389 | 406 | # If you want an inset axes in data-space, you need to manually execute the | |
| 390 | - # layout using `fig.execute_constrained_layout()` call. The inset figure | ||
| 407 | + # layout using ``fig.execute_constrained_layout()`` call. The inset figure | ||
| 391 | 408 | # will then be properly positioned. However, it will not be properly | |
| 392 | 409 | # positioned if the size of the figure is subsequently changed. Similarly, | |
| 393 | 410 | # if the figure is printed to another backend, there may be slight changes | |
@@ -414,7 +431,7 @@ def docomplicated(suptitle=None): | |||
| 414 | 431 | # | |
| 415 | 432 | # ``constrained_layout`` will not work on subplots | |
| 416 | 433 | # created via the `subplot` command. The reason is that each of these | |
| 417 | - # commands creates a separate `GridSpec` instance and `constrained_layout` | ||
| 434 | + # commands creates a separate `GridSpec` instance and ``constrained_layout`` | ||
| 418 | 435 | # uses (nested) gridspecs to carry out the layout. So the following fails | |
| 419 | 436 | # to yield a nice layout: | |
| 420 | 437 | ||
@@ -520,33 +537,33 @@ def docomplicated(suptitle=None): | |||
| 520 | 537 | # | |
| 521 | 538 | # Figures are laid out in a hierarchy: | |
| 522 | 539 | # | |
| 523 | - # 1. Figure: `fig = plt.figure()` | ||
| 540 | + # 1. Figure: ``fig = plt.figure()`` | ||
| 524 | 541 | # | |
| 525 | - # a. Gridspec `gs0 = gridspec.GridSpec(1, 2, figure=fig)` | ||
| 542 | + # a. Gridspec ``gs0 = gridspec.GridSpec(1, 2, figure=fig)`` | ||
| 526 | 543 | # | |
| 527 | - # i. Subplotspec: `ss = gs[0, 0]` | ||
| 544 | + # i. Subplotspec: ``ss = gs[0, 0]`` | ||
| 528 | 545 | # | |
| 529 | - # 1. Axes: `ax0 = fig.add_subplot(ss)` | ||
| 546 | + # 1. Axes: ``ax0 = fig.add_subplot(ss)`` | ||
| 530 | 547 | # | |
| 531 | - # ii. Subplotspec: `ss = gs[0, 1]` | ||
| 548 | + # ii. Subplotspec: ``ss = gs[0, 1]`` | ||
| 532 | 549 | # | |
| 533 | - # 1. Gridspec: `gsR = gridspec.GridSpecFromSubplotSpec(2, 1, ss)` | ||
| 550 | + # 1. Gridspec: ``gsR = gridspec.GridSpecFromSubplotSpec(2, 1, ss)`` | ||
| 534 | 551 | # | |
| 535 | - # - Subplotspec: ss = gsR[0, 0] | ||
| 552 | + # - Subplotspec: ``ss = gsR[0, 0]`` | ||
| 536 | 553 | # | |
| 537 | - # - Axes: `axR0 = fig.add_subplot(ss)` | ||
| 554 | + # - Axes: ``axR0 = fig.add_subplot(ss)`` | ||
| 538 | 555 | # | |
| 539 | - # - Subplotspec: ss = gsR[1, 0] | ||
| 556 | + # - Subplotspec: ``ss = gsR[1, 0]`` | ||
| 540 | 557 | # | |
| 541 | - # - Axes: `axR1 = fig.add_subplot(ss)` | ||
| 558 | + # - Axes: ``axR1 = fig.add_subplot(ss)`` | ||
| 542 | 559 | # | |
| 543 | 560 | # Each item has a layoutbox associated with it. The nesting of gridspecs | |
| 544 | 561 | # created with `.GridSpecFromSubplotSpec` can be arbitrarily deep. | |
| 545 | 562 | # | |
| 546 | - # Each ``~matplotlib.axes.Axes` has *two* layoutboxes. The first one, | ||
| 563 | + # Each `~matplotlib.axes.Axes` has *two* layoutboxes. The first one, | ||
| 547 | 564 | # ``ax._layoutbox`` represents the outside of the Axes and all its | |
| 548 | 565 | # decorations (i.e. ticklabels,axis labels, etc.). | |
| 549 | - # The second layoutbox corresponds to the Axes' `ax.position`, which sets | ||
| 566 | + # The second layoutbox corresponds to the Axes' ``ax.position``, which sets | ||
| 550 | 567 | # where in the figure the spines are placed. | |
| 551 | 568 | # | |
| 552 | 569 | # Why so many stacked containers? Ideally, all that would be needed are the | |
@@ -569,8 +586,8 @@ def docomplicated(suptitle=None): | |||
| 569 | 586 | # the difference between the red ``pos`` box and the green ``ax`` box | |
| 570 | 587 | # is set by the size of the decorations around the Axes. | |
| 571 | 588 | # | |
| 572 | - # In the code, this is accomplished by the entries in `.do_constrained_layout` | ||
| 573 | - # like:: | ||
| 589 | + # In the code, this is accomplished by the entries in | ||
| 590 | + # ``do_constrained_layout()`` like:: | ||
| 574 | 591 | # | |
| 575 | 592 | # ax._poslayoutbox.edit_left_margin_min(-bbox.x0 + pos.x0 + w_padt) | |
| 576 | 593 | # | |
@@ -589,7 +606,7 @@ def docomplicated(suptitle=None): | |||
| 589 | 606 | # much smaller than the left-hand, so the right-hand layoutboxes are smaller. | |
| 590 | 607 | # | |
| 591 | 608 | # The Subplotspec boxes are laid out in the code in the subroutine | |
| 592 | - # `.arange_subplotspecs`, which simply checks the subplotspecs in the code | ||
| 609 | + # ``arange_subplotspecs()``, which simply checks the subplotspecs in the code | ||
| 593 | 610 | # against one another and stacks them appropriately. | |
| 594 | 611 | # | |
| 595 | 612 | # The two ``pos`` axes are lined up. Because they have the same | |
@@ -627,10 +644,10 @@ def docomplicated(suptitle=None): | |||
| 627 | 644 | # number of columns (one in this example). | |
| 628 | 645 | # | |
| 629 | 646 | # The colorbar layout logic is contained in `~matplotlib.colorbar.make_axes` | |
| 630 | - # which calls `._constrained_layout.layoutcolorbarsingle` | ||
| 647 | + # which calls ``_constrained_layout.layoutcolorbarsingle()`` | ||
| 631 | 648 | # for cbars attached to a single axes, and | |
| 632 | - # `._constrained_layout.layoutcolorbargridspec` if the colorbar is associated | ||
| 633 | - # with a gridspec. | ||
| 649 | + # ``_constrained_layout.layoutcolorbargridspec()`` if the colorbar is | ||
| 650 | + # associated with a gridspec. | ||
| 634 | 651 | ||
| 635 | 652 | fig, ax = plt.subplots(1, 2, constrained_layout=True) | |
| 636 | 653 | im = ax[0].pcolormesh(arr, **pc_kwargs) | |
@@ -644,7 +661,7 @@ def docomplicated(suptitle=None): | |||
| 644 | 661 | # | |
| 645 | 662 | # This example shows the Subplotspec layoutboxes being made smaller by | |
| 646 | 663 | # a colorbar layoutbox. The size of the colorbar layoutbox is | |
| 647 | - # set to be *shrink* smaller than the vertical extent of the ``pos`` | ||
| 664 | + # set to be ``shrink`` smaller than the vertical extent of the ``pos`` | ||
| 648 | 665 | # layoutboxes in the gridspec, and it is made to be centered between | |
| 649 | 666 | # those two points. | |
| 650 | 667 | ||
@@ -713,7 +730,7 @@ def docomplicated(suptitle=None): | |||
| 713 | 730 | # the axes position in made the same size as the occupied Axes positions. | |
| 714 | 731 | # | |
| 715 | 732 | # This is done at the start of | |
| 716 | - # `~._constrained_layout.do_constrained_layout` (``hassubplotspec``). | ||
| 733 | + # ``_constrained_layout.do_constrained_layout()`` (``hassubplotspec``). | ||
| 717 | 734 | ||
| 718 | 735 | fig = plt.figure(constrained_layout=True) | |
| 719 | 736 | gs = gridspec.GridSpec(1, 3, figure=fig) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -10,6 +10,9 @@ | |||
| 10 | 10 | feature and may not work for some cases. It only checks the extents | |
| 11 | 11 | of ticklabels, axis labels, and titles. | |
| 12 | 12 | ||
| 13 | + An alternative to *tight_layout* is | ||
| 14 | + :doc:`constrained_layout</tutorials/intermediate/constrainedlayout_guide>`. | ||
| 15 | + | ||
| 13 | 16 | ||
| 14 | 17 | Simple Example | |
| 15 | 18 | ============== | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -91,6 +91,8 @@ | |||
| 91 | 91 | plt.show() | |
| 92 | 92 | ||
| 93 | 93 | ############################################################################### | |
| 94 | + # .. _matplotlib-rcparams: | ||
| 95 | + # | ||
| 94 | 96 | # matplotlib rcParams | |
| 95 | 97 | # =================== | |
| 96 | 98 | # | |
| Back | FazBrowse Home | New Git URL |
0 commit comments