
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "gallery/subplots_axes_and_figures/figure_size_units.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        Click :ref:`here <sphx_glr_download_gallery_subplots_axes_and_figures_figure_size_units.py>`
        to download the full example code

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_gallery_subplots_axes_and_figures_figure_size_units.py:


==============================
Figure size in different units
==============================

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.

.. GENERATED FROM PYTHON SOURCE LINES 11-16

.. code-block:: default



    import matplotlib.pyplot as plt
    text_kwargs = dict(ha='center', va='center', fontsize=28, color='C1')








.. GENERATED FROM PYTHON SOURCE LINES 18-21

Figure size in inches (default)
-------------------------------


.. GENERATED FROM PYTHON SOURCE LINES 21-26

.. code-block:: default

    plt.subplots(figsize=(6, 2))
    plt.text(0.5, 0.5, '6 inches x 2 inches', **text_kwargs)
    plt.show()





.. image-sg:: /gallery/subplots_axes_and_figures/images/sphx_glr_figure_size_units_001.png
   :alt: figure size units
   :srcset: /gallery/subplots_axes_and_figures/images/sphx_glr_figure_size_units_001.png, /gallery/subplots_axes_and_figures/images/sphx_glr_figure_size_units_001_2_0x.png 2.0x
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 27-34

Figure size in centimeter
-------------------------
Multiplying centimeter-based numbers with a conversion factor from cm to
inches, gives the right numbers. Naming the conversion factor ``cm`` makes
the conversion almost look like appending a unit to the number, which is
nicely readable.


.. GENERATED FROM PYTHON SOURCE LINES 34-40

.. code-block:: default

    cm = 1/2.54  # centimeters in inches
    plt.subplots(figsize=(15*cm, 5*cm))
    plt.text(0.5, 0.5, '15cm x 5cm', **text_kwargs)
    plt.show()





.. image-sg:: /gallery/subplots_axes_and_figures/images/sphx_glr_figure_size_units_002.png
   :alt: figure size units
   :srcset: /gallery/subplots_axes_and_figures/images/sphx_glr_figure_size_units_002.png, /gallery/subplots_axes_and_figures/images/sphx_glr_figure_size_units_002_2_0x.png 2.0x
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 41-48

Figure size in pixel
--------------------
Similarly, one can use a conversion from pixels.

Note that you could break this if you use `~.pyplot.savefig` with a
different explicit dpi value.


.. GENERATED FROM PYTHON SOURCE LINES 48-53

.. code-block:: default

    px = 1/plt.rcParams['figure.dpi']  # pixel in inches
    plt.subplots(figsize=(600*px, 200*px))
    plt.text(0.5, 0.5, '600px x 200px', **text_kwargs)
    plt.show()




.. image-sg:: /gallery/subplots_axes_and_figures/images/sphx_glr_figure_size_units_003.png
   :alt: figure size units
   :srcset: /gallery/subplots_axes_and_figures/images/sphx_glr_figure_size_units_003.png, /gallery/subplots_axes_and_figures/images/sphx_glr_figure_size_units_003_2_0x.png 2.0x
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 54-61

Quick interactive work is usually rendered to the screen, making pixels a
good size of unit. But defining the conversion factor may feel a little
tedious for quick iterations.

Because of the default ``rcParams['figure.dpi'] = 100``, one can mentally
divide the needed pixel value by 100 [#]_:


.. GENERATED FROM PYTHON SOURCE LINES 61-65

.. code-block:: default

    plt.subplots(figsize=(6, 2))
    plt.text(0.5, 0.5, '600px x 200px', **text_kwargs)
    plt.show()




.. image-sg:: /gallery/subplots_axes_and_figures/images/sphx_glr_figure_size_units_004.png
   :alt: figure size units
   :srcset: /gallery/subplots_axes_and_figures/images/sphx_glr_figure_size_units_004.png, /gallery/subplots_axes_and_figures/images/sphx_glr_figure_size_units_004_2_0x.png 2.0x
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 66-71

.. [#] Unfortunately, this does not work well for the ``matplotlib inline``
       backend in Jupyter because that backend uses a different default of
       ``rcParams['figure.dpi'] = 72``. Additionally, it saves the figure
       with ``bbox_inches='tight'``, which crops the figure and makes the
       actual size unpredictable.

.. GENERATED FROM PYTHON SOURCE LINES 73-81

.. admonition:: References

   The use of the following functions, methods, classes and modules is shown
   in this example:

   - `matplotlib.pyplot.figure`
   - `matplotlib.pyplot.subplots`
   - `matplotlib.pyplot.subplot_mosaic`


.. _sphx_glr_download_gallery_subplots_axes_and_figures_figure_size_units.py:


.. only :: html

 .. container:: sphx-glr-footer
    :class: sphx-glr-footer-example



  .. container:: sphx-glr-download sphx-glr-download-python

     :download:`Download Python source code: figure_size_units.py <figure_size_units.py>`



  .. container:: sphx-glr-download sphx-glr-download-jupyter

     :download:`Download Jupyter notebook: figure_size_units.ipynb <figure_size_units.ipynb>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    Keywords: matplotlib code example, codex, python plot, pyplot
    `Gallery generated by Sphinx-Gallery
    <https://sphinx-gallery.readthedocs.io>`_
