.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/subplots_axes_and_figures/subfigures.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. meta:: :keywords: codex .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_gallery_subplots_axes_and_figures_subfigures.py: ================= Figure subfigures ================= Sometimes it is desirable to have a figure with two different layouts in it. This can be achieved with :doc:`nested gridspecs`, but having a virtual figure with its own artists is helpful, so Matplotlib also has "subfigures", accessed by calling `matplotlib.figure.Figure.add_subfigure` in a way that is analogous to `matplotlib.figure.Figure.add_subplot`, or `matplotlib.figure.Figure.subfigures` to make an array of subfigures. Note that subfigures can also have their own child subfigures. .. note:: The *subfigure* concept is new in v3.4, and the API is still provisional. .. GENERATED FROM PYTHON SOURCE LINES 20-60 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np def example_plot(ax, fontsize=12, hide_labels=False): pc = ax.pcolormesh(np.random.randn(30, 30), vmin=-2.5, vmax=2.5) if not hide_labels: ax.set_xlabel('x-label', fontsize=fontsize) ax.set_ylabel('y-label', fontsize=fontsize) ax.set_title('Title', fontsize=fontsize) return pc np.random.seed(19680808) # gridspec inside gridspec fig = plt.figure(layout='constrained', figsize=(10, 4)) subfigs = fig.subfigures(1, 2, wspace=0.07) axsLeft = subfigs[0].subplots(1, 2, sharey=True) subfigs[0].set_facecolor('0.75') for ax in axsLeft: pc = example_plot(ax) subfigs[0].suptitle('Left plots', fontsize='x-large') subfigs[0].colorbar(pc, shrink=0.6, ax=axsLeft, location='bottom') axsRight = subfigs[1].subplots(3, 1, sharex=True) for nn, ax in enumerate(axsRight): pc = example_plot(ax, hide_labels=True) if nn == 2: ax.set_xlabel('xlabel') if nn == 1: ax.set_ylabel('ylabel') subfigs[1].set_facecolor('0.85') subfigs[1].colorbar(pc, shrink=0.6, ax=axsRight) subfigs[1].suptitle('Right plots', fontsize='x-large') fig.suptitle('Figure suptitle', fontsize='xx-large') plt.show() .. image-sg:: /gallery/subplots_axes_and_figures/images/sphx_glr_subfigures_001.png :alt: Figure suptitle, Title, Title :srcset: /gallery/subplots_axes_and_figures/images/sphx_glr_subfigures_001.png, /gallery/subplots_axes_and_figures/images/sphx_glr_subfigures_001_2_00x.png 2.00x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 61-64 It is possible to mix subplots and subfigures using `matplotlib.figure.Figure.add_subfigure`. This requires getting the gridspec that the subplots are laid out on. .. GENERATED FROM PYTHON SOURCE LINES 64-89 .. code-block:: Python fig, axs = plt.subplots(2, 3, layout='constrained', figsize=(10, 4)) gridspec = axs[0, 0].get_subplotspec().get_gridspec() # clear the left column for the subfigure: for a in axs[:, 0]: a.remove() # plot data in remaining axes: for a in axs[:, 1:].flat: a.plot(np.arange(10)) # make the subfigure in the empty gridspec slots: subfig = fig.add_subfigure(gridspec[:, 0]) axsLeft = subfig.subplots(1, 2, sharey=True) subfig.set_facecolor('0.75') for ax in axsLeft: pc = example_plot(ax) subfig.suptitle('Left plots', fontsize='x-large') subfig.colorbar(pc, shrink=0.6, ax=axsLeft, location='bottom') fig.suptitle('Figure suptitle', fontsize='xx-large') plt.show() .. image-sg:: /gallery/subplots_axes_and_figures/images/sphx_glr_subfigures_002.png :alt: Figure suptitle, Title, Title :srcset: /gallery/subplots_axes_and_figures/images/sphx_glr_subfigures_002.png, /gallery/subplots_axes_and_figures/images/sphx_glr_subfigures_002_2_00x.png 2.00x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 90-92 Subfigures can have different widths and heights. This is exactly the same example as the first example, but *width_ratios* has been changed: .. GENERATED FROM PYTHON SOURCE LINES 92-119 .. code-block:: Python fig = plt.figure(layout='constrained', figsize=(10, 4)) subfigs = fig.subfigures(1, 2, wspace=0.07, width_ratios=[2, 1]) axsLeft = subfigs[0].subplots(1, 2, sharey=True) subfigs[0].set_facecolor('0.75') for ax in axsLeft: pc = example_plot(ax) subfigs[0].suptitle('Left plots', fontsize='x-large') subfigs[0].colorbar(pc, shrink=0.6, ax=axsLeft, location='bottom') axsRight = subfigs[1].subplots(3, 1, sharex=True) for nn, ax in enumerate(axsRight): pc = example_plot(ax, hide_labels=True) if nn == 2: ax.set_xlabel('xlabel') if nn == 1: ax.set_ylabel('ylabel') subfigs[1].set_facecolor('0.85') subfigs[1].colorbar(pc, shrink=0.6, ax=axsRight) subfigs[1].suptitle('Right plots', fontsize='x-large') fig.suptitle('Figure suptitle', fontsize='xx-large') plt.show() .. image-sg:: /gallery/subplots_axes_and_figures/images/sphx_glr_subfigures_003.png :alt: Figure suptitle, Title, Title :srcset: /gallery/subplots_axes_and_figures/images/sphx_glr_subfigures_003.png, /gallery/subplots_axes_and_figures/images/sphx_glr_subfigures_003_2_00x.png 2.00x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 120-121 Subfigures can be also be nested: .. GENERATED FROM PYTHON SOURCE LINES 121-149 .. code-block:: Python fig = plt.figure(layout='constrained', figsize=(10, 8)) fig.suptitle('fig') subfigs = fig.subfigures(1, 2, wspace=0.07) subfigs[0].set_facecolor('coral') subfigs[0].suptitle('subfigs[0]') subfigs[1].set_facecolor('coral') subfigs[1].suptitle('subfigs[1]') subfigsnest = subfigs[0].subfigures(2, 1, height_ratios=[1, 1.4]) subfigsnest[0].suptitle('subfigsnest[0]') subfigsnest[0].set_facecolor('r') axsnest0 = subfigsnest[0].subplots(1, 2, sharey=True) for nn, ax in enumerate(axsnest0): pc = example_plot(ax, hide_labels=True) subfigsnest[0].colorbar(pc, ax=axsnest0) subfigsnest[1].suptitle('subfigsnest[1]') subfigsnest[1].set_facecolor('g') axsnest1 = subfigsnest[1].subplots(3, 1, sharex=True) axsRight = subfigs[1].subplots(2, 2) plt.show() .. image-sg:: /gallery/subplots_axes_and_figures/images/sphx_glr_subfigures_004.png :alt: fig :srcset: /gallery/subplots_axes_and_figures/images/sphx_glr_subfigures_004.png, /gallery/subplots_axes_and_figures/images/sphx_glr_subfigures_004_2_00x.png 2.00x :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 6.154 seconds) .. _sphx_glr_download_gallery_subplots_axes_and_figures_subfigures.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: subfigures.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: subfigures.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_