{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Figure labels: suptitle, supxlabel, supylabel\n\nEach Axes can have a title (or actually three - one each with *loc* \"left\",\n\"center\", and \"right\"), but is sometimes desirable to give a whole figure\n(or `.SubFigure`) an overall title, using `.Figure.suptitle`.\n\nWe can also add figure-level x- and y-labels using `.Figure.supxlabel` and\n`.Figure.supylabel`.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\nimport numpy as np\n\nfrom matplotlib.cbook import get_sample_data\n\nx = np.linspace(0.0, 5.0, 501)\n\nfig, (ax1, ax2) = plt.subplots(1, 2, layout='constrained', sharey=True)\nax1.plot(x, np.cos(6*x) * np.exp(-x))\nax1.set_title('damped')\nax1.set_xlabel('time (s)')\nax1.set_ylabel('amplitude')\n\nax2.plot(x, np.cos(6*x))\nax2.set_xlabel('time (s)')\nax2.set_title('undamped')\n\nfig.suptitle('Different types of oscillations', fontsize=16)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A global x- or y-label can be set using the `.Figure.supxlabel` and\n`.Figure.supylabel` methods.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "with get_sample_data('Stocks.csv') as file:\n stocks = np.genfromtxt(\n file, delimiter=',', names=True, dtype=None,\n converters={0: lambda x: np.datetime64(x, 'D')}, skip_header=1)\n\nfig, axs = plt.subplots(4, 2, figsize=(9, 5), layout='constrained',\n sharex=True, sharey=True)\nfor nn, ax in enumerate(axs.flat):\n column_name = stocks.dtype.names[1+nn]\n y = stocks[column_name]\n line, = ax.plot(stocks['Date'], y / np.nanmax(y), lw=2.5)\n ax.set_title(column_name, fontsize='small', loc='left')\nfig.supxlabel('Year')\nfig.supylabel('Stock price relative to max')\n\nplt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ ".. tags::\n\n component: figure\n component: title\n plot-type: line\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 }