23

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.

5 Answers 5

23

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.

4
  • 5
    I searched for fontList and found it at ~/.cache/matplotlib/fontList.py3k.cache . Deleting it makes the code above work again.
    – Pierre B
    Jun 20, 2016 at 11:28
  • 2
    Yep, location of matplotlib config files are defined by operating system.
    – Serenity
    Jun 20, 2016 at 11:42
  • On 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. Jun 19, 2017 at 19:43
  • 7
    You can find the dir of the cache by running import matplotlib as mpl; print(mpl.get_cachedir())
    – steven
    Mar 24, 2021 at 2:28
12

I installed fonts in Ubuntu under WSL2 using apt-get and they were not available to matplotlib.

Using matplotlib version 3.4.2 in JupyterLab I had to do the following procedure after installing a new font to make it available.

First, delete the matplotlib cache dir:

import shutil
import matplotlib

shutil.rmtree(matplotlib.get_cachedir())

The cache location can change depending on your installation. The code above guarantees that you will delete the correct one for your kernel.

Now 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))

The code above will display all available fonts for matplotlib.

1
  • 1
    This is neat. Had trouble for nearly an hour or two before I came across this which finally worked
    – Snehangsu
    Apr 7, 2022 at 18:48
9

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

import matplotlib

matplotlib.font_manager._rebuild()
3
  • This seems to be the working solution for the current version of matplotlib. I can't find the fontList cache.
    – ababuji
    Feb 1, 2019 at 18:41
  • 3
    At least in matplotlib v3.3.2 matplotlib.font_manager is not known at import of matplotlib so you need to do import matplotlib.font_manager; matplotlib.font_manager._rebuild() Oct 30, 2020 at 6:00
  • 19
    In matplotlib 3.4.2 font manager has no atribute _rebuild. I also can't locate the font cache.
    – neves
    Aug 29, 2021 at 16:28
7

As @neves noted, matplotlib.font_manager._rebuild() no longer works. As an alternative to manually removing the cache dir, what works as of Matplotlib 3.4.3 is:

import matplotlib.font_manager
matplotlib.font_manager._load_fontmanager(try_read_cache=False)

Here's the Matplotlib source code for that function:

def _load_fontmanager(*, try_read_cache=True):
    fm_path = Path(
        mpl.get_cachedir(), f"fontlist-v{FontManager.__version__}.json")
    if try_read_cache:
        try:
            fm = json_load(fm_path)
        except Exception as exc:
            pass
        else:
            if getattr(fm, "_version", object()) == FontManager.__version__:
                _log.debug("Using fontManager instance from %s", fm_path)
                return fm
    fm = FontManager()
    json_dump(fm, fm_path)
    _log.info("generated new fontManager")
    return fm
3

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)

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.