.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/text_labels_and_annotations/titles_demo.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. meta:: :keywords: codex .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_gallery_text_labels_and_annotations_titles_demo.py: ================= Title positioning ================= Matplotlib can display plot titles centered, flush with the left side of a set of axes, and flush with the right side of a set of axes. .. GENERATED FROM PYTHON SOURCE LINES 10-20 .. code-block:: Python import matplotlib.pyplot as plt plt.plot(range(10)) plt.title('Center Title') plt.title('Left Title', loc='left') plt.title('Right Title', loc='right') plt.show() .. image-sg:: /gallery/text_labels_and_annotations/images/sphx_glr_titles_demo_001.png :alt: Left Title, Center Title, Right Title :srcset: /gallery/text_labels_and_annotations/images/sphx_glr_titles_demo_001.png, /gallery/text_labels_and_annotations/images/sphx_glr_titles_demo_001_2_00x.png 2.00x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 21-23 The vertical position is automatically chosen to avoid decorations (i.e. labels and ticks) on the topmost x-axis: .. GENERATED FROM PYTHON SOURCE LINES 23-40 .. code-block:: Python fig, axs = plt.subplots(1, 2, layout='constrained') ax = axs[0] ax.plot(range(10)) ax.xaxis.set_label_position('top') ax.set_xlabel('X-label') ax.set_title('Center Title') ax = axs[1] ax.plot(range(10)) ax.xaxis.set_label_position('top') ax.xaxis.tick_top() ax.set_xlabel('X-label') ax.set_title('Center Title') plt.show() .. image-sg:: /gallery/text_labels_and_annotations/images/sphx_glr_titles_demo_002.png :alt: Center Title, Center Title :srcset: /gallery/text_labels_and_annotations/images/sphx_glr_titles_demo_002.png, /gallery/text_labels_and_annotations/images/sphx_glr_titles_demo_002_2_00x.png 2.00x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 41-43 Automatic positioning can be turned off by manually specifying the *y* keyword argument for the title or setting :rc:`axes.titley` in the rcParams. .. GENERATED FROM PYTHON SOURCE LINES 43-60 .. code-block:: Python fig, axs = plt.subplots(1, 2, layout='constrained') ax = axs[0] ax.plot(range(10)) ax.xaxis.set_label_position('top') ax.set_xlabel('X-label') ax.set_title('Manual y', y=1.0, pad=-14) plt.rcParams['axes.titley'] = 1.0 # y is in axes-relative coordinates. plt.rcParams['axes.titlepad'] = -14 # pad is in points... ax = axs[1] ax.plot(range(10)) ax.set_xlabel('X-label') ax.set_title('rcParam y') plt.show() .. image-sg:: /gallery/text_labels_and_annotations/images/sphx_glr_titles_demo_003.png :alt: Manual y, rcParam y :srcset: /gallery/text_labels_and_annotations/images/sphx_glr_titles_demo_003.png, /gallery/text_labels_and_annotations/images/sphx_glr_titles_demo_003_2_00x.png 2.00x :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.708 seconds) .. _sphx_glr_download_gallery_text_labels_and_annotations_titles_demo.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: titles_demo.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: titles_demo.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_