.. 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_mathtext_fontfamily_example.py:
===============
Math fontfamily
===============
A simple example showcasing the new *math_fontfamily* parameter that can
be used to change the family of fonts for each individual text
element in a plot.
If no parameter is set, the global value
:rc:`mathtext.fontset` will be used.
.. image:: /gallery/text_labels_and_annotations/images/sphx_glr_mathtext_fontfamily_example_001.png
:alt: $Title\ in\ math\ mode:\ \int_{0}^{\infty } x^2 dx$
:class: sphx-glr-single-img
.. code-block:: default
import matplotlib.pyplot as plt
plt.figure(figsize=(6, 5))
# A simple plot for the background.
plt.plot(range(11), color="0.9")
# A text mixing normal text and math text.
msg = (r"Normal Text. $Text\ in\ math\ mode:\ "
r"\int_{0}^{\infty } x^2 dx$")
# Set the text in the plot.
plt.text(1, 7, msg, size=12, math_fontfamily='cm')
# Set another font for the next text.
plt.text(1, 3, msg, size=12, math_fontfamily='dejavuserif')
# *math_fontfamily* can be used in most places where there is text,
# like in the title:
plt.title(r"$Title\ in\ math\ mode:\ \int_{0}^{\infty } x^2 dx$",
math_fontfamily='stixsans', size=14)
# Note that the normal text is not changed by *math_fontfamily*.
plt.show()
.. _sphx_glr_download_gallery_text_labels_and_annotations_mathtext_fontfamily_example.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: mathtext_fontfamily_example.py `
.. container:: sphx-glr-download sphx-glr-download-jupyter
:download:`Download Jupyter notebook: mathtext_fontfamily_example.ipynb `
.. only:: html
.. rst-class:: sphx-glr-signature
Keywords: matplotlib code example, codex, python plot, pyplot
`Gallery generated by Sphinx-Gallery
`_