{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Align labels and titles\n\nAligning xlabel, ylabel, and title using `.Figure.align_xlabels`,\n`.Figure.align_ylabels`, and `.Figure.align_titles`.\n\n`.Figure.align_labels` wraps the x and y label functions.\n\nWe align the xlabels and ylabels using short calls to `.Figure.align_xlabels`\nand `.Figure.align_ylabels`. We also show a manual way to align the ylabels\nusing the `~.Axis.set_label_coords` method of the yaxis object. Note this requires\nknowing a good offset value which is hardcoded.\n\n.. redirect-from:: /gallery/pyplots/align_ylabels\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\nimport numpy as np\n\nfig, axs = plt.subplots(2, 3, figsize=(8.9, 5.5),\n                        layout='constrained', gridspec_kw={'wspace': 0.1})\n\n# add sample data and labels\nfor ax in axs.flat:\n    scale = 2000 if ax.get_subplotspec().is_first_row() else 1\n    ax.plot(scale * (1 - np.exp(-np.linspace(0, 5, 100))))\n    if ax.get_subplotspec().is_last_row():\n        ax.set_xlabel('xlabel', bbox=dict(facecolor='yellow', pad=5, alpha=0.2))\n    ax.set_ylabel('ylabel', bbox=dict(facecolor='yellow', pad=5, alpha=0.2))\n    ax.set_ylim(0, scale)\n\n# Modify ticks to get different margins in some plots\naxs[0, 0].xaxis.tick_top()\naxs[1, 2].tick_params(axis='x', rotation=55)\naxs[0, 0].set_title('ylabels not aligned')\n\n# Align labels\nfig.align_titles()            # Align titles\nfig.align_xlabels()           # Align all x-axis labels\nfig.align_ylabels(axs[:, 1])  # Align only the second column's y-labels\naxs[0, 1].set_title('fig.align_ylabels()')\n\n# Manually adjust y-labels for the third column\nfor ax in axs[:, 2]:\n    ax.yaxis.set_label_coords(-0.3, 0.5)\naxs[0, 2].set_title('ylabels manually aligned')\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.align_xlabels`\n   - `matplotlib.figure.Figure.align_ylabels`\n   - `matplotlib.figure.Figure.align_labels`\n   - `matplotlib.figure.Figure.align_titles`\n   - `matplotlib.axis.Axis.set_label_coords`\n   - `matplotlib.axes.Axes.plot` / `matplotlib.pyplot.plot`\n   - `matplotlib.axes.Axes.set_title`\n   - `matplotlib.axes.Axes.set_ylabel`\n   - `matplotlib.axes.Axes.set_ylim`\n\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        ".. tags::\n\n   component: label\n   component: title\n   styling: position\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
}