I have a variety of fonts installed on my system (OS X 10.9.1), and am able to use them all with no problems in all of my tools and applications (including my LaTeX installation) but my attempts to specify a specific font in matplotlib
(with text.usetex=False
) produce unexpected and confusing results.
For example
font = {'family': 'sans-serif',
'sans-serif': ['Open Sans', 'Avenir', 'sans-serif']}
plt.rc('font', **font)
results in neither Open Sans nor Avenir being used, though specifying a size (e.g., 'size': 20
) changes the size as expected.
Strangely, specifying a weight not only changes the weight, but can also result in the font changing. For example
font = {'family': 'sans-serif',
'sans-serif': ['Open Sans', 'Avenir', 'sans-serif'],
'weight': 'bold'}
plt.rc('font', **font)
seems to result in Open Sans being used (but as something that looks like extra-bold rather than bold). If I make weight 550 or more I get the same effect; while anythng less results in the wrong font. I get similar odd behavior with other fonts (e.g.. Sika or Gill Sans), and see this regardless of of whether the font is OT, TT, or whether it is indicated as a System or User font.
Why is (only) matplotlib
responding in this strange way. Are there matplotlib
(or Python) settings or configuration options I should be changing to ensure that I get the expected behavior? Is there some other way I should be specifying the font weight, perhaps?
FWIW, here's how Open Sans looks in my font installation:
All of these are found by Python as expected (in /Users/Rax/Library/Fonts/
) by
import matplotlib.font_manager
print matplotlib.font_manager.findSystemFonts(fontpaths=None)
and
font_manager.FontProperties(fname='/Users/Rax/Library/Fonts/OpenSans-ExtraBold.ttf').get_name()
font_manager.FontProperties(fname='/Users/Rax/Library/Fonts/OpenSans-Regular.ttf').get_name()
both report 'Open Sans' as the font name, as expected, with
font_manager.FontProperties(fname='/Users/Rax/Library/Fonts/OpenSans-Regular.ttf').get_weight()
reporting 'normal'.