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

Skip to content

Commit 627d71d

Browse files
committed
DOC: Inline pgf userdemo examples
They are not too useful as examples, since they need to save `.pgf` files, which are not scraped nor exposed in any way from the generated docs.
1 parent c17197c commit 627d71d

File tree

4 files changed

+67
-104
lines changed

4 files changed

+67
-104
lines changed

galleries/examples/userdemo/pgf_fonts.py

Lines changed: 0 additions & 30 deletions
This file was deleted.

galleries/examples/userdemo/pgf_preamble_sgskip.py

Lines changed: 0 additions & 34 deletions
This file was deleted.

galleries/examples/userdemo/pgf_texsystem.py

Lines changed: 0 additions & 30 deletions
This file was deleted.

galleries/users_explain/text/pgf.py

Lines changed: 67 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@
9191
pdf.savefig(fig2)
9292
9393
94+
.. redirect-from:: /gallery/userdemo/pgf_fonts
95+
9496
Font specification
9597
==================
9698
@@ -107,9 +109,29 @@
107109
When saving to ``.pgf``, the font configuration Matplotlib used for the
108110
layout of the figure is included in the header of the text file.
109111
110-
.. literalinclude:: /gallery/userdemo/pgf_fonts.py
111-
:end-before: fig.savefig
112+
.. code-block:: python
113+
114+
import matplotlib.pyplot as plt
115+
116+
plt.rcParams.update({
117+
"font.family": "serif",
118+
# Use LaTeX default serif font.
119+
"font.serif": [],
120+
# Use specific cursive fonts.
121+
"font.cursive": ["Comic Neue", "Comic Sans MS"],
122+
})
112123
124+
fig, ax = plt.subplots(figsize=(4.5, 2.5))
125+
126+
ax.plot(range(5))
127+
128+
ax.text(0.5, 3., "serif")
129+
ax.text(0.5, 2., "monospace", family="monospace")
130+
ax.text(2.5, 2., "sans-serif", family="DejaVu Sans") # Use specific sans font.
131+
ax.text(2.5, 1., "comic", family="cursive")
132+
ax.set_xlabel("µ is not $\\mu$")
133+
134+
.. redirect-from:: /gallery/userdemo/pgf_preamble_sgskip
113135
114136
.. _pgf-preamble:
115137
@@ -122,16 +144,33 @@
122144
if you want to do the font configuration yourself instead of using the fonts
123145
specified in the rc parameters, make sure to disable :rc:`pgf.rcfonts`.
124146
125-
.. only:: html
147+
.. code-block:: python
126148
127-
.. literalinclude:: /gallery/userdemo/pgf_preamble_sgskip.py
128-
:end-before: fig.savefig
149+
import matplotlib as mpl
129150
130-
.. only:: latex
151+
mpl.use("pgf")
152+
import matplotlib.pyplot as plt
131153
132-
.. literalinclude:: /gallery/userdemo/pgf_preamble_sgskip.py
133-
:end-before: import matplotlib.pyplot as plt
154+
plt.rcParams.update({
155+
"font.family": "serif", # use serif/main font for text elements
156+
"text.usetex": True, # use inline math for ticks
157+
"pgf.rcfonts": False, # don't setup fonts from rc parameters
158+
"pgf.preamble": "\n".join([
159+
r"\usepackage{url}", # load additional packages
160+
r"\usepackage{unicode-math}", # unicode math setup
161+
r"\setmainfont{DejaVu Serif}", # serif font via preamble
162+
])
163+
})
134164
165+
fig, ax = plt.subplots(figsize=(4.5, 2.5))
166+
167+
ax.plot(range(5))
168+
169+
ax.set_xlabel("unicode text: я, ψ, €, ü")
170+
ax.set_ylabel(r"\url{https://matplotlib.org}")
171+
ax.legend(["unicode math: $λ=∑_i^∞ μ_i^2$"])
172+
173+
.. redirect-from:: /gallery/userdemo/pgf_texsystem
135174
136175
.. _pgf-texsystem:
137176
@@ -143,9 +182,27 @@
143182
Please note that when selecting pdflatex, the fonts and Unicode handling must
144183
be configured in the preamble.
145184
146-
.. literalinclude:: /gallery/userdemo/pgf_texsystem.py
147-
:end-before: fig.savefig
185+
.. code-block:: python
186+
187+
import matplotlib.pyplot as plt
188+
189+
plt.rcParams.update({
190+
"pgf.texsystem": "pdflatex",
191+
"pgf.preamble": "\n".join([
192+
r"\usepackage[utf8x]{inputenc}",
193+
r"\usepackage[T1]{fontenc}",
194+
r"\usepackage{cmbright}",
195+
]),
196+
})
197+
198+
fig, ax = plt.subplots(figsize=(4.5, 2.5))
199+
200+
ax.plot(range(5))
148201
202+
ax.text(0.5, 3., "serif", family="serif")
203+
ax.text(0.5, 2., "monospace", family="monospace")
204+
ax.text(2.5, 2., "sans-serif", family="sans-serif")
205+
ax.set_xlabel(r"µ is not $\mu$")
149206
150207
.. _pgf-troubleshooting:
151208

0 commit comments

Comments
 (0)