Skip to content Skip to sidebar Skip to footer

Matplotlib Can't Find Font Installed In My Linux Machine

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], 'b

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

defmake_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 insorted(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)

Solution 5:

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
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:
            passelse:
            ifgetattr(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

Post a Comment for "Matplotlib Can't Find Font Installed In My Linux Machine"