I’ve long struggled with modifying the fonts for matplotlib plots from the default “Bitstream Vera Sans” until I found this post recently.
The steps outlined there worked quite well for changing the general font used for the plots, but escaped math characters still would change to the default Computer Modern font. That font isn’t terrible and looks fine in the context of LaTeX documents, but it’s awkward to mix the fonts in a given figure. It turns out that it’s pretty easy to change all of the fonts used for the figures. The examples below show how to set the default font to Helvetica for all fonts, math or otherwise that will be used for the plot.
In ~/.matplotlib/matplotlibrc:
font.family : sans-serif ## Use Helvetica font.sans-serif : Helvetica mathtext.fontset : custom font.size: 12 mathtext.rm : Helvetica mathtext.it : Helvetica:italic mathtext.bf : Helvetica:bold mathtext.sf : Helvetica mathtext.tt : Courier mathtext.cal : Courier:italic
Note that I had to find the “Helvetica.dfont” file with the OSX “Font Book” app and generate the TrueType files with fondu as described in the blogpost above. To find the appropriate matplotlib fonts directory, I used
datapath=`python -c "import matplotlib; print matplotlib.rcParams['datapath']"` cd ${datapath}/fonts/ttf
Some useful updates to the ~/.matplotlib/matplotlibrc file are shown below.
#backend : TkAgg #backend : Macosx backend : Qt4Agg backend_fallback: True interactive : True legend.scatterpoints : 1 legend.fontsize : small axes.titlesize : medium image.interpolation : Nearest ## Heavier ticks & add minor ticks xtick.minor.size : 3.0 xtick.major.size : 6.0 xtick.minor.width : 1.0 xtick.major.width : 1.0 xtick.minor.visible : True ytick.minor.size : 3.0 ytick.major.size : 6.0 ytick.minor.width : 1.0 ytick.major.width : 1.0 ## Square figure.figsize : 6.0, 6.0 ## font.sans-serif : #, Bitstream Vera Sans, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif font.family : sans-serif ## Use Calibri font.sans-serif : Calibri # Calibri needs a bit bigger font.size: 13 mathtext.fontset : custom mathtext.rm : Calibri mathtext.it : Calibri:italic mathtext.bf : Calibri:bold mathtext.sf : Calibri mathtext.tt : Courier mathtext.cal : Courier:italic ## Use Helvetica # font.sans-serif : Helvetica # mathtext.fontset : custom # font.size: 12 # mathtext.rm : Helvetica # mathtext.it : Helvetica:italic # mathtext.bf : Helvetica:bold # mathtext.sf : Helvetica # mathtext.tt : Courier # mathtext.cal : Courier:italic ## Use Times # font.family : serif # font.serif : Times # font.size: 12 # mathtext.fontset : custom # mathtext.rm : Times # mathtext.it : Times:italic # mathtext.bf : Times:bold # mathtext.sf : Times # mathtext.tt : Courier # mathtext.cal : Courier:italic</h3>