Matplotlib can't find font installed in my Linux machine
Solution 1
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
.
Solution 2
For Mac User: try to run this command in python: (or before the .py file)
import matplotlib
matplotlib.font_manager._rebuild()
Solution 3
Using matplotlib version 3.4.2 in JupyterLab I had to do the following procedure after installing a new font in WSL2 to make it available.
First, delete the cache dir:
import shutil
import matplotlib
shutil.rmtree(matplotlib.get_cachedir())
Then restart your notebook kernel.
Then test if the new font appears using this command in a notebook cell:
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))
Solution 4
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)
Pierre B
Updated on June 15, 2022Comments
-
Pierre B almost 2 years
I am trying to draw an xkcd-style plot with matplotlib (ver. 1.4.2) under Python 3.
When I try to run:
import matplotlib.pyplot as plt plt.xkcd() plt.plot([1,2,3,4], [1,4,9,16], 'bo') plt.axis([0, 6, 0, 20]) plt.show()
It opens an empty window without any image and I get the error:
/usr/lib/python3/dist-packages/matplotlib/font_manager.py:1279: UserWarning: findfont: Font family ['Humor Sans', 'Comic Sans MS', 'StayPuft'] not found. Falling back to Bitstream Vera Sans (prop.get_family(), self.defaultFamily[fontext])) /usr/lib/python3/dist-packages/matplotlib/font_manager.py:1289: UserWarning: findfont: Could not match :family=Bitstream Vera Sans:style=normal:variant=normal:weight=normal:stretch=normal:size=medium. Returning /usr/share/matplotlib/mpl-data/fonts/ttf/STIXSizOneSymReg.ttf UserWarning) Exception in Tkinter callback
I have Humor Sans installed. I checked it with
fc-list | grep Humor
. It can also be used within other programs, like Libre Office. I also have staypuft installed. Isn't that enough?The same code above but without the plt.xkcd() bit works flawlessly.
An alternative to plt.show(), like pylab.savefig() won't work either for the xkcd code, but doesn't have any problem with the same code without using xkcd.
-
Pierre B almost 8 yearsI searched for fontList and found it at ~/.cache/matplotlib/fontList.py3k.cache . Deleting it makes the code above work again.
-
Serenity almost 8 yearsYep, location of matplotlib config files are defined by operating system.
-
Tom Johnson almost 7 yearsOn Windows you need to look in %HOMEPATH%\.matplotlib. There's a file fontList.py3k.cache. Delete it. If you are using jupyter notebooks you must restart jupyter before the new fonts are picked up and the cache recreated.
-
imperialgendarme over 5 yearsThis seems to be the working solution for the current version of matplotlib. I can't find the
fontList
cache. -
Matthew Feickert over 3 yearsAt least in
matplotlib
v3.3.2
matplotlib.font_manager
is not known at import ofmatplotlib
so you need to doimport matplotlib.font_manager; matplotlib.font_manager._rebuild()
-
steven about 3 yearsYou can find the dir of the cache by running
import matplotlib as mpl; print(mpl.get_cachedir())
-
Naypa almost 3 yearsIn matplotlib 3.4.2 font manager has no atribute
_rebuild
. I also can't locate the font cache. -
Snehangsu about 2 yearsThis is neat. Had trouble for nearly an hour or two before I came across this which finally worked