# matplotlib pgf backend with lualatex - non-math font does not change

When using Python's matplotlib to produce plots, I sometimes require the lualatex engine using the pgf backend for matplotlib when producing "heavier" plots like hexbin, otherwise the TeX memory is exceeded and pdflatex fails. When using lualatex The math font seems to change according to my pgf.preamble, however, the rest of the font does not, and stays as CM Roman. With pdflatex, the font behaves how I want. Note that if I go to www.overleaf.com and use the same preamble and compile with pdflatex or lualatex, the document shows the fonts I desire, in both math, and main body. EDIT: the main body font actually does not work in overleaf, I was mistaken.

Update: xelatex behaves the same as lualatex, ie. only math fonts are changed.

Update: Solved by using \usepackage{fontspec}\usepackage[T1]{fontenc} at the start of the preamble. I thought fontspec would not do anything with the T1 font encoding, but it's needed and it works.

This question is similar to: How do I use kpfonts with fontspec?

With lua engine:

with pdf engine:

Code:

import matplotlib as mpl

eng = 'lua' # issue with lua only   <<<<<<<<
# eng = 'pdf' # no issue here
# eng = 'xe' # update: issue here as well

mpl.use('pgf')
mpl.rc('font', family='serif')
mpl.rcParams.update({
"pgf.rcfonts"  : False,
"pgf.texsystem": eng + "latex",
"pgf.preamble" : '\\usepackage[utf8x]{inputenc}\\usepackage[light]{kpfonts}',
})

import numpy as np
import matplotlib.pyplot as plt

t = np.linspace(0,6,100)
x = np.sin(t)

fig, ax = plt.subplots()
ax.plot(t, x, label='A line')
plt.title('A function, $f(x) = \sin(x)$')
plt.legend()
plt.savefig('example.pdf')

• Would xetex have the same problem? Jul 27, 2021 at 14:04
• @AlanXiang same issue with xelatex. Btw, I've found your blog to be a great read, thanks! Jul 27, 2021 at 14:49
• Thanks! Really glad that you like it. Jul 28, 2021 at 0:27

What happens if you remove the preamble settings and change example.pdf to example.pgf?. Or is there a reason to use pdf-format?

When I render something in Matplotlib that will be put into Latex, I always choose render with Latex and maybe set some basic settings like (sans-)serif and font size, and choose the output format to be pgf.

Doing this and choosing not to set the explicit font, I can later switch fonts without having to render another figure. This is because the actual plot will be plotted when rendering the pdf from Latex (in your case, when you click "Compile" in Overleaf).

If you do this, I think you have to change \includegraphics{example.pdf} to \input{example.pgf}, since it is LaTeX-code you want to include.

• My reason for using pdf format is compilation speed, portability, archiveability, and the editting and tweaking process is easier for me. I can try to save as .pgf, but my preference is still .pdf Jul 27, 2021 at 14:59
• You can always edit the pgf afterwards (even easier than with a pdf). You can also choose to plot the same picture and save it in two different formats if you need the pdf as well. Jul 27, 2021 at 15:01
• Understood, and good point. I have saved as pfg and used \input in the past, but I've just found that getting it right in matplotlib and saving as a pdf and using includegraphics has been better for my workflow. Jul 27, 2021 at 15:12

Set "pgf.preamble" to:

\usepackage{fontspec}
\usepackage[T1]{fontenc}
\usepackage[utf8x]{inputenc}
\usepackage[light]{kpfonts}