{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Resize Axes with constrained layout\n\n*Constrained layout* attempts to resize subplots in\na figure so that there are no overlaps between Axes objects and labels\non the Axes.\n\nSee `constrainedlayout_guide` for more details and\n`tight_layout_guide` for an alternative.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\n\n\ndef example_plot(ax):\n    ax.plot([1, 2])\n    ax.set_xlabel('x-label', fontsize=12)\n    ax.set_ylabel('y-label', fontsize=12)\n    ax.set_title('Title', fontsize=14)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "If we don't use *constrained layout*, then labels overlap the Axes\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "fig, axs = plt.subplots(nrows=2, ncols=2, layout=None)\n\nfor ax in axs.flat:\n    example_plot(ax)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "adding ``layout='constrained'`` automatically adjusts.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "fig, axs = plt.subplots(nrows=2, ncols=2, layout='constrained')\n\nfor ax in axs.flat:\n    example_plot(ax)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Below is a more complicated example using nested gridspecs.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "fig = plt.figure(layout='constrained')\n\nimport matplotlib.gridspec as gridspec\n\ngs0 = gridspec.GridSpec(1, 2, figure=fig)\n\ngs1 = gridspec.GridSpecFromSubplotSpec(3, 1, subplot_spec=gs0[0])\nfor n in range(3):\n    ax = fig.add_subplot(gs1[n])\n    example_plot(ax)\n\n\ngs2 = gridspec.GridSpecFromSubplotSpec(2, 1, subplot_spec=gs0[1])\nfor n in range(2):\n    ax = fig.add_subplot(gs2[n])\n    example_plot(ax)\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.gridspec.GridSpec`\n   - `matplotlib.gridspec.GridSpecFromSubplotSpec`\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.6"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}