{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Figure size in different units\n\nThe native figure size unit in Matplotlib is inches, deriving from print\nindustry standards. However, users may need to specify their figures in other\nunits like centimeters or pixels. This example illustrates how to do this\nefficiently.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\n\ntext_kwargs = dict(ha='center', va='center', fontsize=28, color='C1')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Figure size in inches (default)\n\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "plt.subplots(figsize=(6, 2))\nplt.text(0.5, 0.5, '6 inches x 2 inches', **text_kwargs)\nplt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Figure size in centimeter\nMultiplying centimeter-based numbers with a conversion factor from cm to\ninches, gives the right numbers. Naming the conversion factor ``cm`` makes\nthe conversion almost look like appending a unit to the number, which is\nnicely readable.\n\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "cm = 1/2.54  # centimeters in inches\nplt.subplots(figsize=(15*cm, 5*cm))\nplt.text(0.5, 0.5, '15cm x 5cm', **text_kwargs)\nplt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Figure size in pixel\nSimilarly, one can use a conversion from pixels.\n\nNote that you could break this if you use `~.pyplot.savefig` with a\ndifferent explicit dpi value.\n\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "px = 1/plt.rcParams['figure.dpi']  # pixel in inches\nplt.subplots(figsize=(600*px, 200*px))\nplt.text(0.5, 0.5, '600px x 200px', **text_kwargs)\nplt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Quick interactive work is usually rendered to the screen, making pixels a\ngood size of unit. But defining the conversion factor may feel a little\ntedious for quick iterations.\n\nBecause of the default ``rcParams['figure.dpi'] = 100``, one can mentally\ndivide the needed pixel value by 100 [#]_:\n\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "plt.subplots(figsize=(6, 2))\nplt.text(0.5, 0.5, '600px x 200px', **text_kwargs)\nplt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        ".. [#] Unfortunately, this does not work well for the ``matplotlib inline``\n       backend in Jupyter because that backend saves the figure\n       with ``bbox_inches='tight'``, which crops the figure and makes the\n       actual size unpredictable.\n\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        ".. admonition:: References\n\n   The use of the following functions, methods, classes and modules is shown\n   in this example:\n\n   - `matplotlib.pyplot.figure`\n   - `matplotlib.pyplot.subplots`\n   - `matplotlib.pyplot.subplot_mosaic`\n\n.. tags::\n\n   component: figure\n   styling: size\n   level: beginner\n\n"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.14.6"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}