Skip to content

Instantly share code, notes, and snippets.

@alexrudy
Last active July 12, 2022 13:16
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexrudy/a7982903a2fb2ab0dde3 to your computer and use it in GitHub Desktop.
Save alexrudy/a7982903a2fb2ab0dde3 to your computer and use it in GitHub Desktop.
Adding custom fonts to Matplotlib on OS X

How to add custom (or system) fonts to matplotlib on OS X

Matplotlib expects to find .ttf fonts on your system. Newer versions of OS X use .dfont files. Converting them is easy, and putting them in a proper font path is also easy. The user font path on OS X is ~/Library/Fonts/, and matplotlib will find fonts here.

  1. First, install what you'll need:
  • matplotlib
  • fondu (port install fondu, or brew install fondu)
  1. Then find your desired font file. For system font files, the easiest way to do this is in the FontBook application. You can open FontBook in your utlities folder. Then select your font of choice, right click on it, and select "Show in Finder". You'll need to know where it is. Many system fonts are in places like /System/Library/Fonts/...
  2. Move to your user's font directory ~/Library/Fonts/.
  3. Use fondu to convert the OS-X specific font file (ends with .dfont) to .ttf.
  4. Delete your matplotlib font cache, rm ~/.matplotlib/fontList.cache
  5. Set your desired font in your matplotlibrc file. That's probably at ~/.matplotlib/matplotlibrc. You can change the font with entries like the following:
# Fonts
font.serif         : Times, Palatino, New Century Schoolbook, Bookman, Computer Modern Roman
font.sans-serif    : Helvetica, Avant Garde, Computer Modern Sans serif
@fsahli
Copy link

fsahli commented Jan 31, 2018

Thanks for posting this! Just add that I had to rebuild the font cache afterwards:

import matplotlib
matplotlib.font_manager._rebuild()

@frankliuao
Copy link

Nice one! Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment