Matplotlib can use LaTeX to render text. This is activated by setting text.usetex : True
in your rcParams, or by setting the usetex
property to True on individual Text
objects. Text handling through LaTeX is slower than Matplotlib's very capable mathtext, but is more flexible, since different LaTeX packages (font packages, math packages, etc.) can be used. The results can be striking, especially when you take care to use the same fonts in your figures as in the main document.
Matplotlib's LaTeX support requires a working LaTeX installation. For the *Agg backends, dvipng is additionally required; for the PS backend, PSfrag, dvips and Ghostscript are additionally required. For the PDF and SVG backends, if LuaTeX is present, it will be used to speed up some post-processing steps, but note that it is not used to parse the TeX string itself (only LaTeX is supported). The executables for these external dependencies must all be located on your PATH
.
Only a small number of font families (defined by the PSNFSS scheme) are supported. They are listed here, with the corresponding LaTeX font selection commands and LaTeX packages, which are automatically used.
The default font family (which does not require loading any LaTeX package) is Computer Modern. All other families are Adobe fonts. Times and Palatino each have their own accompanying math fonts, while the other Adobe serif fonts make use of the Computer Modern math fonts.
To enable LaTeX and select a font, use e.g.:
plt.rcParams.update({ "text.usetex": True, "font.family": "Helvetica" })
or equivalently, set your matplotlibrc to:
text.usetex : true font.family : Helvetica
It is also possible to instead set font.family
to one of the generic family names and then configure the corresponding generic family; e.g.:
plt.rcParams.update({ "text.usetex": True, "font.family": "sans-serif", "font.sans-serif": "Helvetica", })
(this was the required approach until Matplotlib 3.5).
Here is the standard example, Render math equations using TeX:
Note that display math mode ($$ e=mc^2 $$
) is not supported, but adding the command \displaystyle
, as in the above demo, will produce the same results.
Non-ASCII characters (e.g. the degree sign in the y-label above) are supported to the extent that they are supported by inputenc.
Note
For consistency with the non-usetex case, Matplotlib special-cases newlines, so that single-newlines yield linebreaks (rather than being interpreted as whitespace in standard LaTeX).
Matplotlib uses the underscore package so that underscores (_
) are printed "as-is" in text mode (rather than causing an error as in standard LaTeX). Underscores still introduce subscripts in math mode.
Note
Certain characters require special escaping in TeX, such as:
# $ % & ~ ^ \ { } \( \) \[ \]
Therefore, these characters will behave differently depending on rcParams["text.usetex"]
(default: False
). As noted above, underscores (_
) do not require escaping outside of math mode.
Note
LaTeX always defaults to using a serif font for math (even when rcParams["font.family"] = "sans-serif"
). If desired, adding \usepackage{sfmath}
to rcParams["text.latex.preamble"]
lets LaTeX output sans-serif math.
In order to produce encapsulated PostScript (EPS) files that can be embedded in a new LaTeX document, the default behavior of Matplotlib is to distill the output, which removes some PostScript operators used by LaTeX that are illegal in an EPS file. This step produces results which may be unacceptable to some users, because the text is coarsely rasterized and converted to bitmaps, which are not scalable like standard PostScript, and the text is not searchable. One workaround is to set rcParams["ps.distiller.res"]
(default: 6000
) to a higher value (perhaps 6000) in your rc settings, which will produce larger files but may look better and scale reasonably. A better workaround, which requires Poppler or Xpdf, can be activated by changing rcParams["ps.usedistiller"]
(default: None
) to xpdf
. This alternative produces PostScript without rasterizing text, so it scales properly, can be edited in Adobe Illustrator, and searched text in pdf documents.
Try deleting your .matplotlib/tex.cache
directory. If you don't know where to find .matplotlib
, see matplotlib configuration and cache directory locations.
Make sure LaTeX, dvipng, and Ghostscript are each working and on your PATH
. The PATH
environment variable may need to be modified (in particular on Windows) to include the directories containing the executables. See Environment variables and Setting environment variables in Windows for details.
Make sure what you are trying to do is possible in a LaTeX document, that your LaTeX syntax is valid and that you are using raw strings if necessary to avoid unintended escape sequences.
rcParams["text.latex.preamble"]
(default: ''
) is not officially supported. This option provides lots of flexibility, and lots of ways to cause problems. Please disable this option before reporting problems.
Using MiKTeX with Computer Modern fonts, if you get odd *Agg and PNG results, go to MiKTeX/Options and update your format files.
Some required LaTeX packages, such as type1cm, may be missing from minimalist TeX installs. Required packages are listed at LaTeX dependencies.
If you still need help, please see Get help.
Gallery generated by Sphinx-Gallery
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4