2
votes

I need to use a required OTF font while plotting with matplotlib, but cannot figure out how to access it. I saw How to use a (random) *.otf or *.ttf font in matplotlib?, but neither recommended solution works for me.

Option 1, where I set prop = matplotlib.font_manager.FontProperties(fname = '/Users/<username>/Library/Fonts/Univers-Condensed.otf') spits an error immediately because the OTF font doesn't have the TTF structure expected.

Option 2, where I set the family name to the general name of the font, finds the OTF font:

plot_font = {'family' : 'Univers-Condensed',
             'size'   : '11'}

matplotlib.rc('font', **plot_font)

plt.plot(range(10))
plt.title('Show me Universe', size = 32)

plt.show()

Groovy! But when I change plt.show() to plt.savefig('test.pdf') I get this error:

UserWarning: findfont: Font family ['Univers-Condensed'] not found.
Falling back to Bitstream Vera Sans
  (prop.get_family(), self.defaultFamily[fontext]))

Why is plt.savefig() unable to find the same fonts that plt.show() CAN find? Do you have any recommendations for a different way to approach it? I'd prefer not to convert the fonts to TTF.

1
If you write import matplotlib.font_manager from matplotlib import rcParams and rcParams['font.family'] = 'Univers-Condensed' before plt.savefig() does that make a difference?Chuck
github.com/matplotlib/matplotlib/issues/3590 "When you add new fonts to your system, you need to delete your fontList.cache file in order for matplotlib to find them." "The matplotlib font cache can be found here: Win7: %userprofile%\.matplotlib\ Ubuntu: ~/.cache/matplotlib"Chuck
I'm working on OSX, so the paths are a bit different. I had cleared ~/.matplotlib/fontList.py3k.cache, but not ~/.matplotlip/tex.cache. The different behavior seems to come from the continued existence of ~/.matplotlip/tex.cache.Caroline

1 Answers

1
votes

I found this explanation this morning, which helps matplotlib find a family of fonts installed in the user profile. It's important to note that both ~/.matplotlib/fontList.py3k.cache AND ~/.matplotlip/tex.cache need to be cleared. I had only cleared ~/.matplotlib/fontList.py3k.cache during my initial tries.

I still haven't identified WHY plt.show() and plt.savefig() behaved differently when only one cache was cleared, but I am now able to save figures with the fonts that I want.