{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Title positioning\n\nMatplotlib can display plot titles centered, flush with the left side of\na set of Axes, and flush with the right side of a set of Axes.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n\nplt.plot(range(10))\n\nplt.title('Center Title')\nplt.title('Left Title', loc='left')\nplt.title('Right Title', loc='right')\n\nplt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The vertical position is automatically chosen to avoid decorations\n(i.e. labels and ticks) on the topmost x-axis:\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "fig, axs = plt.subplots(1, 2, layout='constrained')\n\nax = axs[0]\nax.plot(range(10))\nax.xaxis.set_label_position('top')\nax.set_xlabel('X-label')\nax.set_title('Center Title')\n\nax = axs[1]\nax.plot(range(10))\nax.xaxis.set_label_position('top')\nax.xaxis.tick_top()\nax.set_xlabel('X-label')\nax.set_title('Center Title')\nplt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Automatic positioning can be turned off by manually specifying the *y*\nkeyword argument for the title or setting :rc:`axes.titley` in the rcParams.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "fig, axs = plt.subplots(1, 2, layout='constrained')\n\nax = axs[0]\nax.plot(range(10))\nax.xaxis.set_label_position('top')\nax.set_xlabel('X-label')\nax.set_title('Manual y', y=1.0, pad=-14)\n\nplt.rcParams['axes.titley'] = 1.0 # y is in axes-relative coordinates.\nplt.rcParams['axes.titlepad'] = -14 # pad is in points...\nax = axs[1]\nax.plot(range(10))\nax.set_xlabel('X-label')\nax.set_title('rcParam y')\n\nplt.show()" ] } ], "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 }