{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Labelling subplots\n\nLabelling subplots is relatively straightforward, and varies, so Matplotlib\ndoes not have a general method for doing this.\n\nWe showcase two methods to position text at a given physical offset (in\nfontsize units or in points) away from a corner of the Axes: one using\n`~.Axes.annotate`, and one using `.ScaledTranslation`.\n\nFor convenience, this example uses `.pyplot.subplot_mosaic` and subplot\nlabels as keys for the subplots. However, the approach also works with\n`.pyplot.subplots` or keys that are different from what you want to label the\nsubplot with.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n\nfrom matplotlib.transforms import ScaledTranslation" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "fig, axs = plt.subplot_mosaic([['a)', 'c)'], ['b)', 'c)'], ['d)', 'd)']],\n layout='constrained')\nfor label, ax in axs.items():\n # Use Axes.annotate to put the label\n # - at the top left corner (axes fraction (0, 1)),\n # - offset half-a-fontsize right and half-a-fontsize down\n # (offset fontsize (+0.5, -0.5)),\n # i.e. just inside the axes.\n ax.annotate(\n label,\n xy=(0, 1), xycoords='axes fraction',\n xytext=(+0.5, -0.5), textcoords='offset fontsize',\n fontsize='medium', verticalalignment='top', fontfamily='serif',\n bbox=dict(facecolor='0.7', edgecolor='none', pad=3.0))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "fig, axs = plt.subplot_mosaic([['a)', 'c)'], ['b)', 'c)'], ['d)', 'd)']],\n layout='constrained')\nfor label, ax in axs.items():\n # Use ScaledTranslation to put the label\n # - at the top left corner (axes fraction (0, 1)),\n # - offset 20 pixels left and 7 pixels up (offset points (-20, +7)),\n # i.e. just outside the axes.\n ax.text(\n 0.0, 1.0, label, transform=(\n ax.transAxes + ScaledTranslation(-20/72, +7/72, fig.dpi_scale_trans)),\n fontsize='medium', va='bottom', fontfamily='serif')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If we want it aligned with the title, either incorporate in the title or\nuse the *loc* keyword argument:\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "fig, axs = plt.subplot_mosaic([['a)', 'c)'], ['b)', 'c)'], ['d)', 'd)']],\n layout='constrained')\nfor label, ax in axs.items():\n ax.set_title('Normal Title', fontstyle='italic')\n ax.set_title(label, fontfamily='serif', loc='left', fontsize='medium')\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.subplot_mosaic` /\n `matplotlib.pyplot.subplot_mosaic`\n - `matplotlib.axes.Axes.set_title`\n - `matplotlib.axes.Axes.annotate`\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 }