Changing matplotlib fonts

Tutorial by: Kimberly Hoogewind

If you're like me, you may be getting sick of using the default font in matplotlib (Bitstream Vera Sans -- boring!). Luckily, changing the font is fairly easy! 

Checking which fonts are available on your system

To check for available fonts, we simply use matplotlib's  font_manager  module. Here is an example that will search for all available system fonts and print them out:

import matplotlib.font_manager as font_manager

#view available fonts
for font in font_manager.findSystemFonts():
  print font


Downloading fonts

If you're not satisfied with any of the available system fonts, you can simply download your own! There are many sites out there which offer free font files. It's easiest to work with true type fonts (.ttf files). I am not very familiar with font types, but I think matplotlib can work with more than just ttf. 

Download your desired fonts and put them in a  .fonts directory in your home space. I recommend making directories 
or each font that you download to avoid overriding any text files of the same name (e.g. OFL.txt files containing Open Font License info) and to help with organization.

Some sites with free font downloads:

Changing your font

When making figures with matplotlib, you have control of every text property, including font size, font weight, color, etc. Many of these can be set with **kwargs in the function call or by setting the defaults in the rc file. This example will show you how to dynamically change the default rc settings as I am assuming you want all text to be of the font you selected .

We ultimately want to set the 'font.family' value in the rc file

Example:

import matplotlib
import matplotlib.font_manager as font_manager

fontpath = '/home/khoogewi/.fonts/PlayfairDisplay/PlayfairDisplay-Regular.ttf'

prop = font_manager.FontProperties(fname=fontpath)
matplotlib.rcParams['font.family'] = prop.get_name()


If you come across an error similar to this:

/apps/rhel6/Python-2.7.2/lib/python2.7/site-packages/matplotlib/font_manager.py:1224: UserWarning: findfont: Font family ['Playfair Display'] not found. Falling back to Bitstream Vera Sans(prop.get_family(), self.defaultFamily[fontext]))

You likely need to clear the font cache. Go to your home directory and into your .matplotlib directory. You simply need to delete the fontList.cache file and the tex.cache directory, and this should take care of the problem.

Permanently changing your default font

If you want to permanently change your default font, you can do so in your matplotlibrc file. This file contains configurations for all kinds of plot properties. To find where your matplotlibrc files is:

import matplotlib

print matplotlib.matplotlib_fname()

If your rc file is located somewhere where you do not have permissions to edit, simply copy over the file to your ~/.matplotlib directory. If you run the code above again, the path should now reflect the new location. Now you will be able to customize your matplotlibrc file as you see fit. For more information on how to customize your rc file: 
http://matplotlib.org/users/customizing.html

Mathematical Text

It is easy to add mathematical symbols and expressions to your plots using matplotlib.


Comments