{ "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\nNote that the xlabel \"XLabel1 1\" would normally be much closer to the\nx-axis, \"YLabel0 0\" would be much closer to the y-axis, and title\n\"Title0 0\" would be much closer to the top of their respective axes.\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, 2, layout='constrained')\n\nax = axs[0][0]\nax.plot(np.arange(0, 1e6, 1000))\nax.set_title('Title0 0')\nax.set_ylabel('YLabel0 0')\n\nax = axs[0][1]\nax.plot(np.arange(1., 0., -0.1) * 2000., np.arange(1., 0., -0.1))\nax.set_title('Title0 1')\nax.xaxis.tick_top()\nax.tick_params(axis='x', rotation=55)\n\n\nfor i in range(2):\n ax = axs[1][i]\n ax.plot(np.arange(1., 0., -0.1) * 2000., np.arange(1., 0., -0.1))\n ax.set_ylabel('YLabel1 %d' % i)\n ax.set_xlabel('XLabel1 %d' % i)\n if i == 0:\n ax.tick_params(axis='x', rotation=55)\n\nfig.align_labels() # same as fig.align_xlabels(); fig.align_ylabels()\nfig.align_titles()\n\nplt.show()" ] }, { "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.13.2" } }, "nbformat": 4, "nbformat_minor": 0 }