Matplotlib can't find font

If you add a new font after installing matplotlib then try to remove the font cache. Matplotlib will have to rebuild the cache, thereby adding the new font.

It may be located under ~/.matplotlib/fontList.cache or ~/.cache/matplotlib/fontList.json.


For Mac User: try to run this command in python: (or before the .py file)

import matplotlib

matplotlib.font_manager._rebuild()

Just in case somebody wants to choose a custom font for their chart. You can manually set up the font for your chart labels, title, legend, or tick labels. The following code demonstrates how to set a custom font for your chart. And the error you mentioned can disappear.

import matplotlib.font_manager as fm
import matplotlib.pyplot as plt

font_path = '/System/Library/Fonts/PingFang.ttc'  # the location of the font file
my_font = fm.FontProperties(fname=font_path)  # get the font based on the font_path

fig, ax = plt.subplots()

ax.bar(x, y, color='green')
ax.set_xlabel(u'Some text', fontproperties=my_font)
ax.set_ylabel(u'Some text', fontproperties=my_font)
ax.set_title(u'title', fontproperties=my_font)
for label in ax.get_xticklabels():
    label.set_fontproperties(my_font)