{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Resize Axes with tight layout\n\n`~.Figure.tight_layout` attempts to resize subplots in a figure so that there\nare no overlaps between Axes objects and labels on the Axes.\n\nSee `tight_layout_guide` for more details and\n`constrainedlayout_guide` for an alternative.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import itertools\nimport warnings\n\nimport matplotlib.pyplot as plt\n\nfontsizes = itertools.cycle([8, 16, 24, 32])\n\n\ndef example_plot(ax):\n    ax.plot([1, 2])\n    ax.set_xlabel('x-label', fontsize=next(fontsizes))\n    ax.set_ylabel('y-label', fontsize=next(fontsizes))\n    ax.set_title('Title', fontsize=next(fontsizes))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "fig, ax = plt.subplots()\nexample_plot(ax)\nfig.tight_layout()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(nrows=2, ncols=2)\nexample_plot(ax1)\nexample_plot(ax2)\nexample_plot(ax3)\nexample_plot(ax4)\nfig.tight_layout()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "fig, (ax1, ax2) = plt.subplots(nrows=2, ncols=1)\nexample_plot(ax1)\nexample_plot(ax2)\nfig.tight_layout()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2)\nexample_plot(ax1)\nexample_plot(ax2)\nfig.tight_layout()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "fig, axs = plt.subplots(nrows=3, ncols=3)\nfor ax in axs.flat:\n    example_plot(ax)\nfig.tight_layout()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "plt.figure()\nax1 = plt.subplot(221)\nax2 = plt.subplot(223)\nax3 = plt.subplot(122)\nexample_plot(ax1)\nexample_plot(ax2)\nexample_plot(ax3)\nplt.tight_layout()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "plt.figure()\nax1 = plt.subplot2grid((3, 3), (0, 0))\nax2 = plt.subplot2grid((3, 3), (0, 1), colspan=2)\nax3 = plt.subplot2grid((3, 3), (1, 0), colspan=2, rowspan=2)\nax4 = plt.subplot2grid((3, 3), (1, 2), rowspan=2)\nexample_plot(ax1)\nexample_plot(ax2)\nexample_plot(ax3)\nexample_plot(ax4)\nplt.tight_layout()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "fig = plt.figure()\n\ngs1 = fig.add_gridspec(3, 1)\nax1 = fig.add_subplot(gs1[0])\nax2 = fig.add_subplot(gs1[1])\nax3 = fig.add_subplot(gs1[2])\nexample_plot(ax1)\nexample_plot(ax2)\nexample_plot(ax3)\ngs1.tight_layout(fig, rect=[None, None, 0.45, None])\n\ngs2 = fig.add_gridspec(2, 1)\nax4 = fig.add_subplot(gs2[0])\nax5 = fig.add_subplot(gs2[1])\nexample_plot(ax4)\nexample_plot(ax5)\nwith warnings.catch_warnings():\n    # gs2.tight_layout cannot handle the subplots from the first gridspec\n    # (gs1), so it will raise a warning. We are going to match the gridspecs\n    # manually so we can filter the warning away.\n    warnings.simplefilter(\"ignore\", UserWarning)\n    gs2.tight_layout(fig, rect=[0.45, None, None, None])\n\n# now match the top and bottom of two gridspecs.\ntop = min(gs1.top, gs2.top)\nbottom = max(gs1.bottom, gs2.bottom)\n\ngs1.update(top=top, bottom=bottom)\ngs2.update(top=top, bottom=bottom)\n\nplt.show()"
      ]
    },
    {
      "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.figure.Figure.tight_layout` /\n     `matplotlib.pyplot.tight_layout`\n   - `matplotlib.figure.Figure.add_gridspec`\n   - `matplotlib.figure.Figure.add_subplot`\n   - `matplotlib.pyplot.subplot2grid`\n\n.. tags::\n\n   component: axes\n   component: subplot\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.7"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}