Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 07e5464

Browse files
author
pwuertz
committed
add documentation for pgf backend, update what's new
1 parent 7938800 commit 07e5464

12 files changed

Lines changed: 214 additions & 0 deletions

doc/users/index_text.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Working with text
88
text_intro.rst
99
text_props.rst
1010
mathtext.rst
11+
pgf.rst
1112
usetex.rst
1213
annotations_intro.rst
1314

doc/users/pgf.rst

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
.. _pgf-tutorial:
2+
3+
*********************************
4+
Typesetting With XeLaTeX/LuaLaTeX
5+
*********************************
6+
7+
Using the ``pgf`` backend, matplotlib can export figures as pgf drawing commands
8+
that can be processed with pdflatex, xelatex or lualatex. XeLaTeX and LuaLaTeX
9+
have full unicode support and can use any fonts installed in the operating
10+
system, making use of advanced typographic features of OpenType, AAT and
11+
Graphite. Pgf pictures created by ``plt.savefig('figure.pgf')`` can be
12+
embedded as raw commands in LaTeX documents. Figures can also be directly
13+
compiled and saved to PDF with ``plt.savefig('figure.pdf')``.
14+
15+
Matplotlib's pgf support requires a working LaTeX_ installation (such as
16+
TeXLive_), preferably including XeLaTeX or LuaLaTeX. If pdftocairo or
17+
ghostscript is installed, figures can optionally be saved to PNG images.
18+
The executables for all applications must be located on your :envvar:`PATH`.
19+
20+
Rc parameters that control the behavior of the pgf backend:
21+
22+
================= =====================================================
23+
Parameter Documentation
24+
================= =====================================================
25+
pgf.preamble Lines to be included in the LaTeX preamble
26+
pgf.rcfonts Setup fonts from rc params using the fontspec package
27+
pgf.texsystem Either "xelatex", "lualatex" or "pdflatex"
28+
================= =====================================================
29+
30+
.. note::
31+
32+
TeX defines a set of secial characters, such as::
33+
34+
# $ % & ~ _ ^ \ { }
35+
36+
Generally, these characters must be escaped correctly. For convenience,
37+
some characters (_,^,%) are automatically escaped outside of math
38+
environments.
39+
40+
.. _pgf-rcfonts:
41+
42+
Font specification
43+
==================
44+
45+
The fonts used for obtaining the size of text elements or when compiling
46+
figures to PDF are usually defined in the matplotlib rc parameters. You can
47+
also use the LaTeX default Computer Modern fonts by clearing the lists for
48+
``font.serif``, ``font.sans-serif`` or ``font.monospace``. Please note that
49+
the glyph coverage of these fonts is very limited. For extended unicode support
50+
the `Computer Modern Unicode <http://sourceforge.net/projects/cm-unicode/>`_
51+
fonts "CMU Serif", "CMU Sans Serif" are recommended.
52+
53+
.. literalinclude:: plotting/examples/pgf_fonts.py
54+
:end-before: plt.savefig
55+
56+
.. image:: plotting/examples/pgf_fonts.*
57+
58+
59+
.. _pgf-preamble:
60+
61+
Custom preamble
62+
===============
63+
64+
Full customization is possible by adding your own commands to the preamble.
65+
Use the ``pgf.preamble`` parameter if you want to configure the math fonts or
66+
for loading additional packages. Also, if you want to do the font configuration
67+
yourself instead of using the fonts specified in the rc parameters, make sure
68+
to disable ``pgf.rcfonts``.
69+
70+
.. htmlonly::
71+
72+
.. literalinclude:: plotting/examples/pgf_preamble.py
73+
:end-before: plt.savefig
74+
75+
.. latexonly::
76+
77+
.. literalinclude:: plotting/examples/pgf_preamble.py
78+
:end-before: import matplotlib.pyplot as plt
79+
80+
.. image:: plotting/examples/pgf_preamble.*
81+
82+
83+
.. _pgf-texsystem:
84+
85+
Choosing the TeX system
86+
=======================
87+
88+
The TeX system to be used by matplotlib is chosen by the ``pgf.texsystem``
89+
parameter. Possible values are ``'xelatex'`` (default), ``'lualatex'`` and
90+
``'pdflatex'``. Please note that when selecting pdflatex the fonts and
91+
unicode handling must be configured in the preamble.
92+
93+
.. literalinclude:: plotting/examples/pgf_texsystem.py
94+
:end-before: plt.savefig
95+
96+
.. image:: plotting/examples/pgf_texsystem.*
97+
98+
.. _pgf-hangups:
99+
100+
Possible hangups
101+
================
102+
103+
* On Windows, the :envvar:`PATH` environment variable may need to be modified
104+
to include the directories containing the latex, dvipng and ghostscript
105+
executables. See :ref:`environment-variables` and
106+
:ref:`setting-windows-environment-variables` for details.
107+
108+
* Sometimes the font rendering in figures that are saved to png images is
109+
very bad. This happens when the pdftocairo tool is not available and
110+
ghostscript is used for the pdf to png conversion.
111+
112+
.. _pgf-troubleshooting:
113+
114+
Troubleshooting
115+
===============
116+
117+
* Make sure what you are trying to do is possible in a LaTeX document,
118+
that your LaTeX syntax is valid and that you are using raw strings
119+
if necessary to avoid unintended escape sequences.
120+
121+
* The ``pgf.preamble`` rc setting provides lots of flexibility, and lots of
122+
ways to cause problems. When experiencing problems, try to minimalize or
123+
disable the custom preamble before reporting problems.
124+
125+
* If you still need help, please see :ref:`reporting-problems`
126+
127+
.. _LaTeX: http://www.tug.org
128+
.. _TeXLive: http://www.tug.org/texlive/
10.4 KB
Binary file not shown.
15 KB
Loading
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import matplotlib as mpl
4+
mpl.use("pgf")
5+
pgf_with_rc_fonts = {
6+
"font.family": "serif",
7+
"font.serif": [], # use latex default serif font
8+
"font.sans-serif": ["DejaVu Sans"], # use a specific sans-serif font
9+
}
10+
mpl.rcParams.update(pgf_with_rc_fonts)
11+
12+
import matplotlib.pyplot as plt
13+
plt.figure(figsize=(4.5,2.5))
14+
plt.plot(range(5))
15+
plt.text(0.5, 3., "serif")
16+
plt.text(0.5, 2., "monospace", family="monospace")
17+
plt.text(2.5, 2., "sans-serif", family="sans-serif")
18+
plt.text(2.5, 1., "comic sans", family="Comic Sans MS")
19+
plt.xlabel(u"µ is not $\\mu$")
20+
plt.tight_layout(.5)
21+
22+
plt.savefig("pgf_fonts.pdf")
23+
plt.savefig("pgf_fonts.png")
14.9 KB
Binary file not shown.
16.7 KB
Loading
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import matplotlib as mpl
4+
mpl.use("pgf")
5+
pgf_with_custom_preamble = {
6+
"font.family": "serif", # use serif/main font for text elements
7+
"text.usetex": True, # use inline math for ticks
8+
"pgf.rcfonts": False, # don't setup fonts from rc parameters
9+
"pgf.preamble": [
10+
r"\usepackage{units}", # load additional packages
11+
r"\usepackage{metalogo}", # load additional packages
12+
r"\usepackage{unicode-math}", # unicode math setup
13+
r"\setmathfont{XITS Math}",
14+
r"\setmainfont{DejaVu Serif}", # font setup via preamble
15+
]
16+
}
17+
mpl.rcParams.update(pgf_with_custom_preamble)
18+
19+
import matplotlib.pyplot as plt
20+
plt.figure(figsize=(4.5,2.5))
21+
plt.plot(range(5))
22+
plt.xlabel(u"unicode text: я, ψ, €, ü, \\unitfrac[10]{°}{µm}")
23+
plt.ylabel(u"\\XeLaTeX")
24+
plt.legend([u"unicode math: $λ=∑_i^∞ μ_i^2$"])
25+
plt.tight_layout(.5)
26+
27+
plt.savefig("pgf_preamble.pdf")
28+
plt.savefig("pgf_preamble.png")
18.7 KB
Binary file not shown.
11.4 KB
Loading

0 commit comments

Comments
 (0)