I am searching for advice on how to handle selecting a specific font, and using that in a matplotlib figure. As a background, the font will be picked through the wx.FontDialog (common font dialog) provided by wxPython. So, what I will have is the font face (Arial, Times New Roman, Algerian, etc. etc.), the weight, the style (italic, normal) and the point size. All I want to do is create a matplotlib font that matches this, and use it in the plot. My first try was this:
import matplotlib.pyplot as plt
import matplotlib.font_manager as fm
this does not work. The title font is wrong.
the_font = ‘Century Schoolbook’
fp = fm.FontProperties()
fp.set_name(the_font)
fp.set_size(24)
plt.title(‘The Title that should be in Century Schoolbook’,fontproperties=fp)
print fp
plt.show()
But that didn’t work. I know that “Century Schoolbook” is not really a font family, but in the docs it says that you can list a font there.
The following does work, if I manually set the TrueType file explicitly:
fp = fm.FontProperties()
fp.set_file(‘c:\Windows\Fonts\CENSCBK.TTF’)
fp.set_size(24)
print fp
plt.title(‘The Title that is in Century Schoolbook’,fontproperties=fp)
plt.show()
So I guess the question is…how does one accomplish this, portably? I don’t quite understand the ins and outs of fonts…
p.s.
I did take a stab at creating a mapping between the font names / weights / styles like this:
all_fontfiles = fm.win32InstalledFonts()
allfonts = fm.createFontList(all_fontfiles)
fontdict = {}
for f in allfonts:
fontdict[(f.name,f.style,f.weight)] = f.fname
And I think I can get this to work, because this maps me to a TTF file for any name, style, and weight combination. But this seemed awfully hacky, and I don’t know what problems I’ll run into on other platforms (obviously, I would have get all_fontfiles above differently on each platform). If this is the only way to do it, I guess that’s OK, but I thought that surely there was a better way.
Thanks,
···
–
Daniel Hyams
dhyams@…287…