1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
|
r"""
.. redirect-from:: /tutorials/text/pgf
.. _pgf:
************************************************************
Text rendering with XeLaTeX/LuaLaTeX via the ``pgf`` backend
************************************************************
Using the ``pgf`` backend, Matplotlib can export figures as pgf drawing
commands that can be processed with pdflatex, xelatex or lualatex. XeLaTeX and
LuaLaTeX have full Unicode support and can use any font that is installed in
the operating system, making use of advanced typographic features of OpenType,
AAT and Graphite. Pgf pictures created by ``plt.savefig('figure.pgf')``
can be embedded as raw commands in LaTeX documents. Figures can also be
directly compiled and saved to PDF with ``plt.savefig('figure.pdf')`` by
switching the backend ::
matplotlib.use('pgf')
or by explicitly requesting the use of the ``pgf`` backend ::
plt.savefig('figure.pdf', backend='pgf')
or by registering it for handling pdf output ::
from matplotlib.backends.backend_pgf import FigureCanvasPgf
matplotlib.backend_bases.register_backend('pdf', FigureCanvasPgf)
The last method allows you to keep using regular interactive backends and to
save xelatex, lualatex or pdflatex compiled PDF files from the graphical user
interface. Note that, in that case, the interactive display will still use the
standard interactive backends (e.g., QtAgg), and in particular use latex to
compile relevant text snippets.
Matplotlib's pgf support requires a recent LaTeX_ installation that includes
the TikZ/PGF packages (such as TeXLive_), preferably with XeLaTeX or LuaLaTeX
installed. If either pdftocairo or ghostscript is present on your system,
figures can optionally be saved to PNG images as well. The executables
for all applications must be located on your :envvar:`PATH`.
`.rcParams` that control the behavior of the pgf backend:
================= =====================================================
Parameter Documentation
================= =====================================================
pgf.preamble Lines to be included in the LaTeX preamble
pgf.rcfonts Setup fonts from rc params using the fontspec package
pgf.texsystem Either "xelatex" (default), "lualatex" or "pdflatex"
================= =====================================================
.. note::
TeX defines a set of special characters, such as::
# $ % & ~ _ ^ \ { }
Generally, these characters must be escaped correctly. For convenience,
some characters (_, ^, %) are automatically escaped outside of math
environments. Other characters are not escaped as they are commonly needed
in actual TeX expressions. However, one can configure TeX to treat them as
"normal" characters (known as "catcode 12" to TeX) via a custom preamble,
such as::
plt.rcParams["pgf.preamble"] = (
r"\AtBeginDocument{\catcode`\&=12\catcode`\#=12}")
.. _pgf-rcfonts:
Multi-Page PDF Files
====================
The pgf backend also supports multipage pdf files using
`~.backend_pgf.PdfPages`
.. code-block:: python
from matplotlib.backends.backend_pgf import PdfPages
import matplotlib.pyplot as plt
with PdfPages('multipage.pdf', metadata={'author': 'Me'}) as pdf:
fig1, ax1 = plt.subplots()
ax1.plot([1, 5, 3])
pdf.savefig(fig1)
fig2, ax2 = plt.subplots()
ax2.plot([1, 5, 3])
pdf.savefig(fig2)
.. redirect-from:: /gallery/userdemo/pgf_fonts
Font specification
==================
The fonts used for obtaining the size of text elements or when compiling
figures to PDF are usually defined in the `.rcParams`. You can also use the
LaTeX default Computer Modern fonts by clearing the lists for :rc:`font.serif`,
:rc:`font.sans-serif` or :rc:`font.monospace`. Please note that the glyph
coverage of these fonts is very limited. If you want to keep the Computer
Modern font face but require extended Unicode support, consider installing the
`Computer Modern Unicode`__ fonts *CMU Serif*, *CMU Sans Serif*, etc.
__ https://sourceforge.net/projects/cm-unicode/
When saving to ``.pgf``, the font configuration Matplotlib used for the
layout of the figure is included in the header of the text file.
.. code-block:: python
import matplotlib.pyplot as plt
plt.rcParams.update({
"font.family": "serif",
# Use LaTeX default serif font.
"font.serif": [],
# Use specific cursive fonts.
"font.cursive": ["Comic Neue", "Comic Sans MS"],
})
fig, ax = plt.subplots(figsize=(4.5, 2.5))
ax.plot(range(5))
ax.text(0.5, 3., "serif")
ax.text(0.5, 2., "monospace", family="monospace")
ax.text(2.5, 2., "sans-serif", family="DejaVu Sans") # Use specific sans font.
ax.text(2.5, 1., "comic", family="cursive")
ax.set_xlabel("µ is not $\\mu$")
.. redirect-from:: /gallery/userdemo/pgf_preamble_sgskip
.. _pgf-preamble:
Custom preamble
===============
Full customization is possible by adding your own commands to the preamble.
Use :rc:`pgf.preamble` if you want to configure the math fonts,
using ``unicode-math`` for example, or for loading additional packages. Also,
if you want to do the font configuration yourself instead of using the fonts
specified in the rc parameters, make sure to disable :rc:`pgf.rcfonts`.
.. code-block:: python
import matplotlib as mpl
mpl.use("pgf")
import matplotlib.pyplot as plt
plt.rcParams.update({
"font.family": "serif", # use serif/main font for text elements
"text.usetex": True, # use inline math for ticks
"pgf.rcfonts": False, # don't setup fonts from rc parameters
"pgf.preamble": "\n".join([
r"\usepackage{url}", # load additional packages
r"\usepackage{unicode-math}", # unicode math setup
r"\setmainfont{DejaVu Serif}", # serif font via preamble
])
})
fig, ax = plt.subplots(figsize=(4.5, 2.5))
ax.plot(range(5))
ax.set_xlabel("unicode text: я, ψ, €, ü")
ax.set_ylabel(r"\url{https://matplotlib.org}")
ax.legend(["unicode math: $λ=∑_i^∞ μ_i^2$"])
.. redirect-from:: /gallery/userdemo/pgf_texsystem
.. _pgf-texsystem:
Choosing the TeX system
=======================
The TeX system to be used by Matplotlib is chosen by :rc:`pgf.texsystem`.
Possible values are ``'xelatex'`` (default), ``'lualatex'`` and ``'pdflatex'``.
Please note that when selecting pdflatex, the fonts and Unicode handling must
be configured in the preamble.
.. code-block:: python
import matplotlib.pyplot as plt
plt.rcParams.update({
"pgf.texsystem": "pdflatex",
"pgf.preamble": "\n".join([
r"\usepackage[utf8x]{inputenc}",
r"\usepackage[T1]{fontenc}",
r"\usepackage{cmbright}",
]),
})
fig, ax = plt.subplots(figsize=(4.5, 2.5))
ax.plot(range(5))
ax.text(0.5, 3., "serif", family="serif")
ax.text(0.5, 2., "monospace", family="monospace")
ax.text(2.5, 2., "sans-serif", family="sans-serif")
ax.set_xlabel(r"µ is not $\mu$")
.. _pgf-troubleshooting:
Troubleshooting
===============
* On Windows, the :envvar:`PATH` environment variable may need to be modified
to include the directories containing the latex, dvipng and ghostscript
executables. See :ref:`environment-variables` and
:ref:`setting-windows-environment-variables` for details.
* Sometimes the font rendering in figures that are saved to png images is
very bad. This happens when the pdftocairo tool is not available and
ghostscript is used for the pdf to png conversion.
* 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.
* :rc:`pgf.preamble` provides lots of flexibility, and lots of
ways to cause problems. When experiencing problems, try to minimalize or
disable the custom preamble.
* Configuring an ``unicode-math`` environment can be a bit tricky. The
TeXLive distribution for example provides a set of math fonts which are
usually not installed system-wide. XeLaTeX, unlike LuaLaTeX, cannot find
these fonts by their name, which is why you might have to specify
``\setmathfont{xits-math.otf}`` instead of ``\setmathfont{XITS Math}`` or
alternatively make the fonts available to your OS. See this
`tex.stackexchange.com question`__ for more details.
__ https://tex.stackexchange.com/q/43642/
* If the font configuration used by Matplotlib differs from the font setting
in yout LaTeX document, the alignment of text elements in imported figures
may be off. Check the header of your ``.pgf`` file if you are unsure about
the fonts Matplotlib used for the layout.
* Vector images and hence ``.pgf`` files can become bloated if there are a lot
of objects in the graph. This can be the case for image processing or very
big scatter graphs. In an extreme case this can cause TeX to run out of
memory: "TeX capacity exceeded, sorry" You can configure latex to increase
the amount of memory available to generate the ``.pdf`` image as discussed on
`tex.stackexchange.com <https://tex.stackexchange.com/q/7953/>`_.
Another way would be to "rasterize" parts of the graph causing problems
using either the ``rasterized=True`` keyword, or ``.set_rasterized(True)`` as
per :doc:`this example </gallery/misc/rasterization_demo>`.
* Various math fonts are compiled and rendered only if corresponding font
packages are loaded. Specifically, when using ``\mathbf{}`` on Greek letters,
the default computer modern font may not contain them, in which case the
letter is not rendered. In such scenarios, the ``lmodern`` package should be
loaded.
* If you still need help, please see :ref:`reporting-problems`
.. _LaTeX: http://www.tug.org
.. _TeXLive: http://www.tug.org/texlive/
"""
|