1

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.

snsunx
  • 155
  • 1
  • 9

1 Answers1

0

I had this issue! Trying to install atlas typewriter. I partially used matplotlib font not found although I did not have a cache to delete, instead there were two json files (fontlist-330.json). I manually added an entry to both jsons and it worked.

  {
      "fname": "/Users/XXUSERNAMEXX/Library/Fonts/AtlasTypewriter-Regular.ttf",
      "name": "Atlas Typewriter",
      "style": "normal",
      "variant": "normal",
      "weight": "normal",
      "stretch": "normal",
      "size": "scalable",
      "__class__": "FontEntry"
    },
Grace
  • 67
  • 1
  • 8