

.. _sphx_glr_gallery_subplots_axes_and_figures:

.. _subplots_axes_and_figures_examples:

Subplots, axes and figures
==========================



.. raw:: html

    <div class="sphx-glr-thumbnails">

.. thumbnail-parent-div-open

.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="Aligning xlabel, ylabel, and title using Figure.align_xlabels, Figure.align_ylabels, and Figure.align_titles.">

.. only:: html

  .. image:: /gallery/subplots_axes_and_figures/images/thumb/sphx_glr_align_labels_demo_thumb.png
    :alt:

  :ref:`sphx_glr_gallery_subplots_axes_and_figures_align_labels_demo.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Align labels and titles</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="    This example is primarily intended to show some advanced concepts in     Matplotlib.">

.. only:: html

  .. image:: /gallery/subplots_axes_and_figures/images/thumb/sphx_glr_auto_subplots_adjust_thumb.png
    :alt:

  :ref:`sphx_glr_gallery_subplots_axes_and_figures_auto_subplots_adjust.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Programmatically control subplot adjustment</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="This demo shows how to set the aspect of an Axes box directly via set_box_aspect. The box aspect is the ratio between Axes height and Axes width in physical units, independent of the data limits. This is useful to e.g. produce a square plot, independent of the data it contains, or to have a usual plot with the same axes dimensions next to an image plot with fixed (data-)aspect.">

.. only:: html

  .. image:: /gallery/subplots_axes_and_figures/images/thumb/sphx_glr_axes_box_aspect_thumb.png
    :alt:

  :ref:`sphx_glr_gallery_subplots_axes_and_figures_axes_box_aspect.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Axes box aspect</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="Example use of fig.add_axes to create inset Axes within the main plot Axes.">

.. only:: html

  .. image:: /gallery/subplots_axes_and_figures/images/thumb/sphx_glr_axes_demo_thumb.png
    :alt:

  :ref:`sphx_glr_gallery_subplots_axes_and_figures_axes_demo.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Axes Demo</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="The first figure in this example shows how to zoom in and out of a plot using margins instead of set_xlim and set_ylim. The second figure demonstrates the concept of edge &quot;stickiness&quot; introduced by certain methods and artists and how to effectively work around that.">

.. only:: html

  .. image:: /gallery/subplots_axes_and_figures/images/thumb/sphx_glr_axes_margins_thumb.png
    :alt:

  :ref:`sphx_glr_gallery_subplots_axes_and_figures_axes_margins.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Controlling view limits using margins and sticky_edges</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="You can control the axis tick and grid properties">

.. only:: html

  .. image:: /gallery/subplots_axes_and_figures/images/thumb/sphx_glr_axes_props_thumb.png
    :alt:

  :ref:`sphx_glr_gallery_subplots_axes_and_figures_axes_props.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Axes properties</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="Axes zoom effect">

.. only:: html

  .. image:: /gallery/subplots_axes_and_figures/images/thumb/sphx_glr_axes_zoom_effect_thumb.png
    :alt:

  :ref:`sphx_glr_gallery_subplots_axes_and_figures_axes_zoom_effect.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Axes zoom effect</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="axhspan and axvspan draw rectangles that span the Axes in either the horizontal or vertical direction and are bounded in the other direction. They are often used to highlight data regions.">

.. only:: html

  .. image:: /gallery/subplots_axes_and_figures/images/thumb/sphx_glr_axhspan_demo_thumb.png
    :alt:

  :ref:`sphx_glr_gallery_subplots_axes_and_figures_axhspan_demo.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Draw regions that span an Axes</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="How to set and adjust plots with equal axis aspect ratios.">

.. only:: html

  .. image:: /gallery/subplots_axes_and_figures/images/thumb/sphx_glr_axis_equal_demo_thumb.png
    :alt:

  :ref:`sphx_glr_gallery_subplots_axes_and_figures_axis_equal_demo.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Equal axis aspect ratio</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="Choose axis label position when calling set_xlabel and set_ylabel as well as for colorbar.">

.. only:: html

  .. image:: /gallery/subplots_axes_and_figures/images/thumb/sphx_glr_axis_labels_demo_thumb.png
    :alt:

  :ref:`sphx_glr_gallery_subplots_axes_and_figures_axis_labels_demo.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Axis label position</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="Broken axis example, where the y-axis will have a portion cut out.">

.. only:: html

  .. image:: /gallery/subplots_axes_and_figures/images/thumb/sphx_glr_broken_axis_thumb.png
    :alt:

  :ref:`sphx_glr_gallery_subplots_axes_and_figures_broken_axis.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Broken axis</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="You can pass a Figure subclass to pyplot.figure if you want to change the default behavior of the figure.">

.. only:: html

  .. image:: /gallery/subplots_axes_and_figures/images/thumb/sphx_glr_custom_figure_class_thumb.png
    :alt:

  :ref:`sphx_glr_gallery_subplots_axes_and_figures_custom_figure_class.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Custom Figure subclasses</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="Constrained layout attempts to resize subplots in a figure so that there are no overlaps between Axes objects and labels on the Axes.">

.. only:: html

  .. image:: /gallery/subplots_axes_and_figures/images/thumb/sphx_glr_demo_constrained_layout_thumb.png
    :alt:

  :ref:`sphx_glr_gallery_subplots_axes_and_figures_demo_constrained_layout.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Resize Axes with constrained layout</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="tight_layout attempts to resize subplots in a figure so that there are no overlaps between Axes objects and labels on the Axes.">

.. only:: html

  .. image:: /gallery/subplots_axes_and_figures/images/thumb/sphx_glr_demo_tight_layout_thumb.png
    :alt:

  :ref:`sphx_glr_gallery_subplots_axes_and_figures_demo_tight_layout.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Resize Axes with tight layout</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="Demo of how to display two scales on the left and right y-axis.">

.. only:: html

  .. image:: /gallery/subplots_axes_and_figures/images/thumb/sphx_glr_fahrenheit_celsius_scales_thumb.png
    :alt:

  :ref:`sphx_glr_gallery_subplots_axes_and_figures_fahrenheit_celsius_scales.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Different scales on the same Axes</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="The native figure size unit in Matplotlib is inches, deriving from print industry standards. However, users may need to specify their figures in other units like centimeters or pixels. This example illustrates how to do this efficiently.">

.. only:: html

  .. image:: /gallery/subplots_axes_and_figures/images/thumb/sphx_glr_figure_size_units_thumb.png
    :alt:

  :ref:`sphx_glr_gallery_subplots_axes_and_figures_figure_size_units.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Figure size in different units</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="Each Axes can have a title (or actually three - one each with loc &quot;left&quot;, &quot;center&quot;, and &quot;right&quot;), but is sometimes desirable to give a whole figure (or SubFigure) an overall title, using Figure.suptitle.">

.. only:: html

  .. image:: /gallery/subplots_axes_and_figures/images/thumb/sphx_glr_figure_title_thumb.png
    :alt:

  :ref:`sphx_glr_gallery_subplots_axes_and_figures_figure_title.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Figure labels: suptitle, supxlabel, supylabel</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="To create plots that share a common axis (visually) you can set the hspace between the subplots to zero. Passing sharex=True when creating the subplots will automatically turn off all x ticks and labels except those on the bottom axis.">

.. only:: html

  .. image:: /gallery/subplots_axes_and_figures/images/thumb/sphx_glr_ganged_plots_thumb.png
    :alt:

  :ref:`sphx_glr_gallery_subplots_axes_and_figures_ganged_plots.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Adjacent subplots</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="This shows 4 possible geographic projections.  Cartopy_ supports more projections.">

.. only:: html

  .. image:: /gallery/subplots_axes_and_figures/images/thumb/sphx_glr_geo_demo_thumb.png
    :alt:

  :ref:`sphx_glr_gallery_subplots_axes_and_figures_geo_demo.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Geographic Projections</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="Sometimes we want to combine two subplots in an Axes layout created with subplots.  We can get the GridSpec from the Axes and then remove the covered Axes and fill the gap with a new bigger Axes. Here we create a layout with the bottom two Axes in the last column combined.">

.. only:: html

  .. image:: /gallery/subplots_axes_and_figures/images/thumb/sphx_glr_gridspec_and_subplots_thumb.png
    :alt:

  :ref:`sphx_glr_gallery_subplots_axes_and_figures_gridspec_and_subplots.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Combine two subplots using subplots and GridSpec</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="This example demonstrates the use of GridSpec to generate subplots, the control of the relative sizes of subplots with width_ratios and height_ratios, and the control of the spacing around and between subplots using subplot params (left, right, bottom, top, wspace, and hspace).">

.. only:: html

  .. image:: /gallery/subplots_axes_and_figures/images/thumb/sphx_glr_gridspec_customization_thumb.png
    :alt:

  :ref:`sphx_glr_gallery_subplots_axes_and_figures_gridspec_customization.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">GridSpec with variable sizes and spacing</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="GridSpec is a flexible way to layout subplot grids.  Here is an example with a 3x3 grid, and axes spanning all three columns, two columns, and two rows.">

.. only:: html

  .. image:: /gallery/subplots_axes_and_figures/images/thumb/sphx_glr_gridspec_multicolumn_thumb.png
    :alt:

  :ref:`sphx_glr_gallery_subplots_axes_and_figures_gridspec_multicolumn.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Gridspec for multi-column/row subplot layouts</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="GridSpecs can be nested, so that a subplot from a parent GridSpec can set the position for a nested grid of subplots.">

.. only:: html

  .. image:: /gallery/subplots_axes_and_figures/images/thumb/sphx_glr_gridspec_nested_thumb.png
    :alt:

  :ref:`sphx_glr_gallery_subplots_axes_and_figures_gridspec_nested.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Nested Gridspecs</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="This example demonstrates two ways to invert the direction of an axis:">

.. only:: html

  .. image:: /gallery/subplots_axes_and_figures/images/thumb/sphx_glr_invert_axes_thumb.png
    :alt:

  :ref:`sphx_glr_gallery_subplots_axes_and_figures_invert_axes.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Inverted axis</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="matplotlib.pyplot uses the concept of a current figure and current Axes. Figures are identified via a figure number that is passed to figure. The figure with the given number is set as current figure. Additionally, if no figure with the number exists, a new one is created.">

.. only:: html

  .. image:: /gallery/subplots_axes_and_figures/images/thumb/sphx_glr_multiple_figs_demo_thumb.png
    :alt:

  :ref:`sphx_glr_gallery_subplots_axes_and_figures_multiple_figs_demo.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Manage multiple figures in pyplot</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="Sometimes we want a secondary axis on a plot, for instance to convert radians to degrees on the same plot.  We can do this by making a child axes with only one axis visible via axes.Axes.secondary_xaxis and axes.Axes.secondary_yaxis.  This secondary axis can have a different scale than the main axis by providing both a forward and an inverse conversion function in a tuple to the functions keyword argument:">

.. only:: html

  .. image:: /gallery/subplots_axes_and_figures/images/thumb/sphx_glr_secondary_axis_thumb.png
    :alt:

  :ref:`sphx_glr_gallery_subplots_axes_and_figures_secondary_axis.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Secondary Axis</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="It&#x27;s common to make two or more plots which share an axis, e.g., two subplots with time as a common axis.  When you pan and zoom around on one, you want the other to move around with you.  To facilitate this, matplotlib Axes support a sharex and sharey attribute.  When you create a subplot or axes, you can pass in a keyword indicating what Axes you want to share with.">

.. only:: html

  .. image:: /gallery/subplots_axes_and_figures/images/thumb/sphx_glr_share_axis_lims_views_thumb.png
    :alt:

  :ref:`sphx_glr_gallery_subplots_axes_and_figures_share_axis_lims_views.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Share axis limits and views</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="You can share the x- or y-axis limits for one axis with another by passing an Axes instance as a sharex or sharey keyword argument.">

.. only:: html

  .. image:: /gallery/subplots_axes_and_figures/images/thumb/sphx_glr_shared_axis_demo_thumb.png
    :alt:

  :ref:`sphx_glr_gallery_subplots_axes_and_figures_shared_axis_demo.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Shared axis</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="Sometimes it is desirable to have a figure with two different layouts in it. This can be achieved with nested gridspecs&lt;/gallery/subplots_axes_and_figures/gridspec_nested&gt;, but having a virtual figure with its own artists is helpful, so Matplotlib also has &quot;subfigures&quot;, 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.">

.. only:: html

  .. image:: /gallery/subplots_axes_and_figures/images/thumb/sphx_glr_subfigures_thumb.png
    :alt:

  :ref:`sphx_glr_gallery_subplots_axes_and_figures_subfigures.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Figure subfigures</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="Simple demo with multiple subplots.">

.. only:: html

  .. image:: /gallery/subplots_axes_and_figures/images/thumb/sphx_glr_subplot_thumb.png
    :alt:

  :ref:`sphx_glr_gallery_subplots_axes_and_figures_subplot.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Multiple subplots</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="This example demonstrates the use of pyplot.subplot2grid to generate subplots.  Using GridSpec, as demonstrated in /gallery/subplots_axes_and_figures/gridspec_customization is generally preferred.">

.. only:: html

  .. image:: /gallery/subplots_axes_and_figures/images/thumb/sphx_glr_subplot2grid_thumb.png
    :alt:

  :ref:`sphx_glr_gallery_subplots_axes_and_figures_subplot2grid.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">subplot2grid</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="Adjusting the spacing of margins and subplots using pyplot.subplots_adjust.">

.. only:: html

  .. image:: /gallery/subplots_axes_and_figures/images/thumb/sphx_glr_subplots_adjust_thumb.png
    :alt:

  :ref:`sphx_glr_gallery_subplots_axes_and_figures_subplots_adjust.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Subplots spacings and margins</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="pyplot.subplots creates a figure and a grid of subplots with a single call, while providing reasonable control over how the individual plots are created. For more advanced use cases you can use GridSpec for a more general subplot layout or Figure.add_subplot for adding subplots at arbitrary locations within the figure.">

.. only:: html

  .. image:: /gallery/subplots_axes_and_figures/images/thumb/sphx_glr_subplots_demo_thumb.png
    :alt:

  :ref:`sphx_glr_gallery_subplots_axes_and_figures_subplots_demo.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Create multiple subplots using plt.subplots</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="Two plots on the same Axes with different left and right scales.">

.. only:: html

  .. image:: /gallery/subplots_axes_and_figures/images/thumb/sphx_glr_two_scales_thumb.png
    :alt:

  :ref:`sphx_glr_gallery_subplots_axes_and_figures_two_scales.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Plots with different scales</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="Example of an inset Axes and a rectangle showing where the zoom is located.">

.. only:: html

  .. image:: /gallery/subplots_axes_and_figures/images/thumb/sphx_glr_zoom_inset_axes_thumb.png
    :alt:

  :ref:`sphx_glr_gallery_subplots_axes_and_figures_zoom_inset_axes.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Zoom region inset Axes</div>
    </div>


.. thumbnail-parent-div-close

.. raw:: html

    </div>


.. toctree::
   :hidden:

   /gallery/subplots_axes_and_figures/align_labels_demo
   /gallery/subplots_axes_and_figures/auto_subplots_adjust
   /gallery/subplots_axes_and_figures/axes_box_aspect
   /gallery/subplots_axes_and_figures/axes_demo
   /gallery/subplots_axes_and_figures/axes_margins
   /gallery/subplots_axes_and_figures/axes_props
   /gallery/subplots_axes_and_figures/axes_zoom_effect
   /gallery/subplots_axes_and_figures/axhspan_demo
   /gallery/subplots_axes_and_figures/axis_equal_demo
   /gallery/subplots_axes_and_figures/axis_labels_demo
   /gallery/subplots_axes_and_figures/broken_axis
   /gallery/subplots_axes_and_figures/custom_figure_class
   /gallery/subplots_axes_and_figures/demo_constrained_layout
   /gallery/subplots_axes_and_figures/demo_tight_layout
   /gallery/subplots_axes_and_figures/fahrenheit_celsius_scales
   /gallery/subplots_axes_and_figures/figure_size_units
   /gallery/subplots_axes_and_figures/figure_title
   /gallery/subplots_axes_and_figures/ganged_plots
   /gallery/subplots_axes_and_figures/geo_demo
   /gallery/subplots_axes_and_figures/gridspec_and_subplots
   /gallery/subplots_axes_and_figures/gridspec_customization
   /gallery/subplots_axes_and_figures/gridspec_multicolumn
   /gallery/subplots_axes_and_figures/gridspec_nested
   /gallery/subplots_axes_and_figures/invert_axes
   /gallery/subplots_axes_and_figures/multiple_figs_demo
   /gallery/subplots_axes_and_figures/secondary_axis
   /gallery/subplots_axes_and_figures/share_axis_lims_views
   /gallery/subplots_axes_and_figures/shared_axis_demo
   /gallery/subplots_axes_and_figures/subfigures
   /gallery/subplots_axes_and_figures/subplot
   /gallery/subplots_axes_and_figures/subplot2grid
   /gallery/subplots_axes_and_figures/subplots_adjust
   /gallery/subplots_axes_and_figures/subplots_demo
   /gallery/subplots_axes_and_figures/two_scales
   /gallery/subplots_axes_and_figures/zoom_inset_axes

