In the documentation I found an example for a PGF preamble:
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$"])
fig.tight_layout(pad=.5)
fig.savefig("pgf_preamble.pdf")
fig.savefig("pgf_preamble.png")
I noticed that this does not work if I replace the line
fig, ax = plt.subplots(figsize=(4.5, 2.5))
with
from matplotlib.figure import Figure
fig = Figure(figsize=(4.5, 2.5))
ax = fig.add_subplot()
and I don’t understand why this happens. What does work is if I only output a pgf file instead of a png and a pdf file but only if I comment out this line:
fig.tight_layout(pad=.5)
Can anyone tell me what is happening here?