|
91 | 91 | pdf.savefig(fig2)
|
92 | 92 |
|
93 | 93 |
|
| 94 | +.. redirect-from:: /gallery/userdemo/pgf_fonts |
| 95 | +
|
94 | 96 | Font specification
|
95 | 97 | ==================
|
96 | 98 |
|
|
107 | 109 | When saving to ``.pgf``, the font configuration Matplotlib used for the
|
108 | 110 | layout of the figure is included in the header of the text file.
|
109 | 111 |
|
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 | + }) |
112 | 123 |
|
| 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 |
113 | 135 |
|
114 | 136 | .. _pgf-preamble:
|
115 | 137 |
|
|
122 | 144 | if you want to do the font configuration yourself instead of using the fonts
|
123 | 145 | specified in the rc parameters, make sure to disable :rc:`pgf.rcfonts`.
|
124 | 146 |
|
125 |
| -.. only:: html |
| 147 | +.. code-block:: python |
126 | 148 |
|
127 |
| - .. literalinclude:: /gallery/userdemo/pgf_preamble_sgskip.py |
128 |
| - :end-before: fig.savefig |
| 149 | + import matplotlib as mpl |
129 | 150 |
|
130 |
| -.. only:: latex |
| 151 | + mpl.use("pgf") |
| 152 | + import matplotlib.pyplot as plt |
131 | 153 |
|
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 | + }) |
134 | 164 |
|
| 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 |
135 | 174 |
|
136 | 175 | .. _pgf-texsystem:
|
137 | 176 |
|
|
143 | 182 | Please note that when selecting pdflatex, the fonts and Unicode handling must
|
144 | 183 | be configured in the preamble.
|
145 | 184 |
|
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)) |
148 | 201 |
|
| 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$") |
149 | 206 |
|
150 | 207 | .. _pgf-troubleshooting:
|
151 | 208 |
|
|
0 commit comments