.. only:: html
.. note::
:class: sphx-glr-download-link-note
Click :ref:`here ` to download the full example code
.. rst-class:: sphx-glr-example-title
.. _sphx_glr_gallery_text_labels_and_annotations_font_file.py:
===================================
Using a ttf font file in Matplotlib
===================================
Although it is usually not a good idea to explicitly point to a single ttf file
for a font instance, you can do so by passing a `pathlib.Path` instance as the
*font* parameter. Note that passing paths as `str`\s is intentionally not
supported, but you can simply wrap `str`\s in `pathlib.Path`\s as needed.
Here, we use the Computer Modern roman font (``cmr10``) shipped with
Matplotlib.
For a more flexible solution, see
:doc:`/gallery/text_labels_and_annotations/font_family_rc_sgskip` and
:doc:`/gallery/text_labels_and_annotations/fonts_demo`.
.. code-block:: default
from pathlib import Path
import matplotlib as mpl
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
fpath = Path(mpl.get_data_path(), "fonts/ttf/cmr10.ttf")
ax.set_title(f'This is a special font: {fpath.name}', font=fpath)
ax.set_xlabel('This is the default font')
plt.show()
.. image:: /gallery/text_labels_and_annotations/images/sphx_glr_font_file_001.png
:alt: This is a special font: cmr10.ttf
:class: sphx-glr-single-img
------------
References
""""""""""
The use of the following functions, methods, classes and modules is shown
in this example:
.. code-block:: default
import matplotlib
matplotlib.axes.Axes.set_title
.. rst-class:: sphx-glr-script-out
Out:
.. code-block:: none
.. _sphx_glr_download_gallery_text_labels_and_annotations_font_file.py:
.. only :: html
.. container:: sphx-glr-footer
:class: sphx-glr-footer-example
.. container:: sphx-glr-download sphx-glr-download-python
:download:`Download Python source code: font_file.py `
.. container:: sphx-glr-download sphx-glr-download-jupyter
:download:`Download Jupyter notebook: font_file.ipynb `
.. only:: html
.. rst-class:: sphx-glr-signature
Keywords: matplotlib code example, codex, python plot, pyplot
`Gallery generated by Sphinx-Gallery
`_