Claridge-Chang Lab
  • About
  • People
  • Papers
  • Join
  • Blog
  • Contact

How to use custom fonts in Matplotlib

1/24/2017

4 Comments

 
After getting sick of the default font in matplotlib, and frankly annoyed by how difficult it seems to use custom fonts, Joses sat down and figured it out. 
​

These instructions below are for Mac OS X, and assume you have installed Jupyter.

1. Install homebrew: go to and follow the instruction (no plural) here http://brew.sh/

2. Install font converter `fondu` with homebrew using the command:
brew install fondu
in Terminal. We will used `fondu` to convert the font of choice to 'ttf'.

3. Then find the font file you want to add. Open FontBook in your Utilities folder, find your font of choice, right-click on it, and select "Show in Finder".

4. Copy the font family to the folder "\Library\Fonts".

5. If this font already has the file extension `ttf`, you can skip to step 6.
If the font has file extension `dfont`, enter the following lines in Terminal:
cd \Library\Fonts
fondu <filename of font>
This will convert the .dfont file to a .ttf file.

6. Now we need to delete the font cache for matplotlib. In terminal, enter the lines
cd ~/.matplotlib
ls -l

to navigate to the matplotlib temp directory and list its contents.

7. There should be a file named either `fontList.cache` or `fontList.py3k.cache`. Use
rm <name of fontlist cache>
to delete it.

8. Now we need to edit the matplotlib parameters file. Start a new Python notebook in Jupyter and run the command
import matplotlib
matplotlib.matplotlib_fname()

in a new cell. This should give you something like:
'/Users/YourUserName/anaconda/lib/python3.5/site-packages/matplotlib/mpl-data/matplotlibrc’
which is the location of the parameters file.

9. Open this file in a text editor and scroll down to the Font section.

10. Find the line beginning with `font.sans-serif` and/or `font.serif`. Remove the '#' at the beginning of these lines.

11. Add the name of the font family to these lines. For example:
font.serif          : Alegreya, Droid Serif, Bitstream Vera Serif, ...
font.sans-serif     : Helvetica Neue, Source Sans Pro, Bitstream Vera Sans…

I have added Alegreya and Droid Serif to the serif family of fonts; Helvetica Neue and Source Sans Pro have been added to the sans-serif family. Matplotlib will look for fonts in running order, so put the fonts you want to use as default immediately after the colon.

11. Save the edited `matplotlibrc` file.

12. Restart your Jupyter notebook and run the following lines.
import matplotlib.pyplot as plt
plt.rcParams

and scroll to the Font section again to check that your settings have taken effect.

13. You can use the following matplotlib commands to change font family (sans-serif or serif) and the font. For example:
plt.rcParams['font.family'] = 'serif'
plt.rcParams['font.serif'] = 'Droid Serif’

You'll need to restart the notebook and run these lines again if you change the font.

14. To ensure that your edited parameters file will not be ignored when using seaborn, you need to import seaborn as
import seaborn.apionly as sns
This is only possible with the latest version of seaborn (v0.7.1).

-Joses Ho 
4 Comments
Ilsa
3/1/2018 08:25:44 am

Thank you!! I followed these steps (mostly the same) in windows and it really helped me!

Reply
Sonam Jain link
7/4/2018 03:08:29 am


Worthful Python tutorial. Appreciate a lot for taking up the pain to write such a quality content on Python tutorial. Just now I watched this similar Python tutorial and I think this will enhance the knowledge of other visitors for sure. Thanks anyway.:-https://www.youtube.com/watch?v=HcsvDObzW2U

Reply
Zoe link
4/29/2019 07:25:55 am

Thanks for the font explanation!

Reply
seaelem
6/21/2019 08:18:02 pm

OK that was brilliant thank you.

Reply



Leave a Reply.

    Author

    ACCLab

    Archives

    September 2022
    March 2022
    February 2022
    October 2021
    September 2021
    September 2019
    March 2019
    June 2018
    May 2018
    April 2018
    March 2018
    August 2017
    January 2017
    December 2016
    October 2016
    September 2016
    August 2016
    July 2016
    May 2016
    August 2015
    July 2015
    June 2015
    May 2015
    January 2015
    December 2014
    March 2014

    Categories

    All

    RSS Feed