.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/subplots_axes_and_figures/colorbar_placement.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. meta:: :keywords: codex .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_gallery_subplots_axes_and_figures_colorbar_placement.py: ================= Placing Colorbars ================= Colorbars indicate the quantitative extent of image data. Placing in a figure is non-trivial because room needs to be made for them. The simplest case is just attaching a colorbar to each axes: .. GENERATED FROM PYTHON SOURCE LINES 11-27 .. code-block:: default import matplotlib.pyplot as plt import numpy as np # Fixing random state for reproducibility np.random.seed(19680801) fig, axs = plt.subplots(2, 2) cmaps = ['RdBu_r', 'viridis'] for col in range(2): for row in range(2): ax = axs[row, col] pcm = ax.pcolormesh(np.random.random((20, 20)) * (col + 1), cmap=cmaps[col]) fig.colorbar(pcm, ax=ax) .. image-sg:: /gallery/subplots_axes_and_figures/images/sphx_glr_colorbar_placement_001.png :alt: colorbar placement :srcset: /gallery/subplots_axes_and_figures/images/sphx_glr_colorbar_placement_001.png, /gallery/subplots_axes_and_figures/images/sphx_glr_colorbar_placement_001_2_0x.png 2.0x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 28-31 The first column has the same type of data in both rows, so it may be desirable to combine the colorbar which we do by calling `.Figure.colorbar` with a list of axes instead of a single axes. .. GENERATED FROM PYTHON SOURCE LINES 31-41 .. code-block:: default fig, axs = plt.subplots(2, 2) cmaps = ['RdBu_r', 'viridis'] for col in range(2): for row in range(2): ax = axs[row, col] pcm = ax.pcolormesh(np.random.random((20, 20)) * (col + 1), cmap=cmaps[col]) fig.colorbar(pcm, ax=axs[:, col], shrink=0.6) .. image-sg:: /gallery/subplots_axes_and_figures/images/sphx_glr_colorbar_placement_002.png :alt: colorbar placement :srcset: /gallery/subplots_axes_and_figures/images/sphx_glr_colorbar_placement_002.png, /gallery/subplots_axes_and_figures/images/sphx_glr_colorbar_placement_002_2_0x.png 2.0x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 42-45 Relatively complicated colorbar layouts are possible using this paradigm. Note that this example works far better with ``constrained_layout=True`` .. GENERATED FROM PYTHON SOURCE LINES 45-55 .. code-block:: default fig, axs = plt.subplots(3, 3, constrained_layout=True) for ax in axs.flat: pcm = ax.pcolormesh(np.random.random((20, 20))) fig.colorbar(pcm, ax=axs[0, :2], shrink=0.6, location='bottom') fig.colorbar(pcm, ax=[axs[0, 2]], location='bottom') fig.colorbar(pcm, ax=axs[1:, :], location='right', shrink=0.6) fig.colorbar(pcm, ax=[axs[2, 1]], location='left') .. image-sg:: /gallery/subplots_axes_and_figures/images/sphx_glr_colorbar_placement_003.png :alt: colorbar placement :srcset: /gallery/subplots_axes_and_figures/images/sphx_glr_colorbar_placement_003.png, /gallery/subplots_axes_and_figures/images/sphx_glr_colorbar_placement_003_2_0x.png 2.0x :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 56-61 Colorbars with fixed-aspect-ratio axes ====================================== Placing colorbars for axes with a fixed aspect ratio pose a particular challenge as the parent axes changes size depending on the data view. .. GENERATED FROM PYTHON SOURCE LINES 61-76 .. code-block:: default fig, axs = plt.subplots(2, 2, constrained_layout=True) cmaps = ['RdBu_r', 'viridis'] for col in range(2): for row in range(2): ax = axs[row, col] pcm = ax.pcolormesh(np.random.random((20, 20)) * (col + 1), cmap=cmaps[col]) if col == 0: ax.set_aspect(2) else: ax.set_aspect(1/2) if row == 1: fig.colorbar(pcm, ax=ax, shrink=0.6) .. image-sg:: /gallery/subplots_axes_and_figures/images/sphx_glr_colorbar_placement_004.png :alt: colorbar placement :srcset: /gallery/subplots_axes_and_figures/images/sphx_glr_colorbar_placement_004.png, /gallery/subplots_axes_and_figures/images/sphx_glr_colorbar_placement_004_2_0x.png 2.0x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 77-80 One way around this issue is to use an `.Axes.inset_axes` to locate the axes in axes coordinates. Note that if you zoom in on the axes, and change the shape of the axes, the colorbar will also change position. .. GENERATED FROM PYTHON SOURCE LINES 80-97 .. code-block:: default fig, axs = plt.subplots(2, 2, constrained_layout=True) cmaps = ['RdBu_r', 'viridis'] for col in range(2): for row in range(2): ax = axs[row, col] pcm = ax.pcolormesh(np.random.random((20, 20)) * (col + 1), cmap=cmaps[col]) if col == 0: ax.set_aspect(2) else: ax.set_aspect(1/2) if row == 1: cax = ax.inset_axes([1.04, 0.2, 0.05, 0.6]) fig.colorbar(pcm, ax=ax, cax=cax) plt.show() .. image-sg:: /gallery/subplots_axes_and_figures/images/sphx_glr_colorbar_placement_005.png :alt: colorbar placement :srcset: /gallery/subplots_axes_and_figures/images/sphx_glr_colorbar_placement_005.png, /gallery/subplots_axes_and_figures/images/sphx_glr_colorbar_placement_005_2_0x.png 2.0x :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 4.244 seconds) .. _sphx_glr_download_gallery_subplots_axes_and_figures_colorbar_placement.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: colorbar_placement.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: colorbar_placement.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_