Jupyter Notebook — xkcd font not found error & matplotlib.font_manager.__rebuild()
You should know, from the crazy hours of research I put in today (like others have done before me), I found this is an issue that has been going on for at least six (6) years. With multiple people putting in valiant efforts across MULTIPLE platforms for a good resolution. But of course, not everyone has the same set up. And then too, you have the matplotlib being updated over the years. Unfortunately, the blah, blah font not found issue was never really resolved for most unfortunate users.
So, as of Dec 8, 2024, for those searching for these odd jupyter notebook errors for the infamous xkcd font not found and for the __rebuild() font cache errors, using the following to rebuild the font cache (it comes up in multiple forums):
from matplotlib import font_manager
matplotlib.font_manager.__rebuild()
— — well guess what? That _ _ rebuild(), it no longer works. (NOTE: I intentionally put a space in betwixt the two dashes to ensure you know that it is TWO dashes…)
And I have matplotlib 3.9.2. To get the _ _ rebuild() to work, found the following solution to rebuild the font cache, it should help you — add the following into jupyter notebook:
import matplotlib.font_manager
matplotlib.font_manager._load_fontmanager(try_read_cache=False)
It is on stackoverflow of all places (a site I REALLY DESPISE): https://stackoverflow.com/questions/37920935/matplotlib-cant-find-font-installed-in-my-linux-machine/70647041#70647041
The above worked on my Windows 11 and Jupyter Notebook 3.12.5
.
Now, to get the “xkcd font not found & Humor Sans font not found” to work — I added the following to jupyter notebook — — content gained from github:
***************** begin copy ****************
import matplotlib.pyplot as plt
from matplotlib import font_manager
font_dirs = “/path to your ttf file/Humor-Sans.ttf” # The path to the custom font file.
font_files = font_manager.findSystemFonts(fontpaths=font_dirs)
for font_file in font_files:
font_manager.fontManager.addfont(font_file) # <<=== indent this line
***************** end copy ****************
.
— for the plot, added the next set of content I picked up from another site:
***************** begin copy ****************
plt.rcParams[‘font.family’] = ‘Humor Sans’
plt.xlabel(‘X-axis’, fontname=’Humor Sans’)
plt.ylabel(‘Y-axis’, fontname=’Humor Sans’)
***************** end copy ****************
.
There were other items I installed, such as:
- !pip install xkcd
.
You ‘may’ need to verify where that TTF file is located, in Jupyter Notebook (or python in a terminal), use:
fm.findSystemFonts()
.
— — then, new cell:
from matplotlib.font_manager import FontProperties, findfont
fp = FontProperties(family=’Humor Sans’,
style=’normal’, # <<=== indent this line
variant=’normal’, # <<=== indent this line
weight=’normal’, # <<=== indent this line
stretch=’normal’, # <<=== indent this line
size=’medium’) # <<=== indent this line
font = findfont(fp)
.
— — then, new cell — you ‘might’ need this in your cell for the plot:
%matplotlib inline
.
.
tags: addfont, font, font cache, humor sans, Jupyter Notebook, matplotlib, matplotlib.font_manager.__rebuild(), plt rcparams, xkcd font, Xkcd font not found