This is a small tutorial on how to install custom matplotlib fonts (on OSX) in each environment seperately.
First, we need to install our custom fonts. Note: Matplotlib expects a font in True Type format (.ttf). For example, if we want to add the Helvetica font, we need to check if we have the font in .ttf format installed on our system otherwise we need to download it and install it.
One easy way to install fonts (with .ttf extension) is to use 1001fonts. Search for the font you want, and download away.
After downloading the font(s), you have to ensure that matplotlib can recognize the list of all available fonts. Add the downloaded font(s) to /Library/Fonts/.
Ensure that the font(s) is/are added correctly by running the following command in a python shell. This lists all the available fonts that matplotlib can detect within your OS.
import matplotlib.font_manager
matplotlib.font_manager.findSystemFonts(fontpaths=None, fontext='ttf')If you can find your font in this list, then you are good to go. If not, ensure that you added the font in the correct location and rerun the command.
Now that we have added the font in the right location, we have to rebuild the fonts by updating the font cache.
import matplotlib
matplotlib.font_manager._rebuild()Now, we can check if the added font(s) was/were properly build to use within matplotlib. Run the following in a Jupyter notebook:
import matplotlib.font_manager
from IPython.core.display import HTML
def make_html(fontname):
return "<p>{font}: <span style='font-family:{font}; font-size: 24px;'>{font}</p>".format(font=fontname)
code = "\n".join([make_html(font) for font in sorted(set([f.name for f in matplotlib.font_manager.fontManager.ttflist]))])
HTML("<div style='column-count: 2;'>{}</div>".format(code))To ensure that the font can be used, we can run the following test. Suppose that we want to plot a bar chart and change the labels and ticks font.
Note: You will have to change the font.serif parameter below to the font you want to test. Check the name recognized by matplotlib using the HTML command above.
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
df = pd.DataFrame({'perc': pd.Series([45, 35, 10, 5, 3, 2], index=['A', 'B', 'C','D','E','F'])})
# specify the custom font to use
plt.rcParams['font.family'] = 'serif'
plt.rcParams['font.serif'] = 'cmr10' # replace with your font name
fig, ax = plt.subplots(figsize=(7,4))
df.iloc[::-1].plot(kind='barh', legend = False, ax=ax)
ax.set_xlabel('Percentage',fontsize=15)
ax.set_ylabel('Type',fontsize=15)It should produce something like this:

[1] https://stackoverflow.com/questions/37920935/matplotlib-cant-find-font
[2] https://scentellegher.github.io/visualization/2018/05/02/custom-fonts-matplotlib.html