I'm trying to use non-default fonts in matplotlib but keep on getting this warning/error.
findfont: Font family ['sans-serif'] not found. Falling back to DejaVu Sans
If I do
from matplotlib import pyplot as plt
plt.rcParams['font.family'] = 'sans-serif'
plt.rcParams['font.sans-serif'] = 'Helvetica'
fig, ax = plt.subplots()
ax.plot([1, 2, 3], [1, 2, 3], label='some label')
ax.legend()
ax.text(0.5, 0.5, 'some text', transform=ax.transAxes, size=25)
fig.savefig('text.png')
I get the error and both legend and text are in Dejavu Sans
.
However, if I load tex
by adding the extra line when setting up rcParams
plt.rcParams['text.usetex'] = True
the warning still occurs if I have ax.legend()
(but goes away if I don't have ax.legend()
), although both the legend and the text are actually in Helvetica even if I get the warning.
Can someone tell me why this happens? And what should I do to use non-default fonts when not loading tex
? I tried
sudo apt install msttcorefonts -qq
rm ~/.cache/matplotlib -rf
or uncomment the lines in matplotlibrc
and rebuild but none of those worked.