I'm intermittently getting the warning font family ['serif'] not found. Falling back to DejaVu Sans when plotting with rc('text', usetex=True) enabled. If I delete ~\.cache\matplotlib the warning stops and the charts are rendered with the correct font. But a day or so later it starts again
Code for reproduction
Cannot reliably reproduce as intermittent, can reliably fix by deleting ~\.cache\matplotlib
Can you please provide your ~/.cache/matplotlib/fontlist-v300.json before and after deleting ~/.cache/matplotlib (and reimporting matplotlib to regenerate the json)? Thanks.
Reacted by David Waterworth, Allan Leal, Nairo Leon, Jinghong Yang and Shibo Tan
I've been playing with a minimal notebook - if I restart kernel and clear outputs, the first time I run the cells below I get the warnings, the 2nd time there's no warning and the 2nd font found is different.
i.e. first run through after restarting kernel:
import matplotlib.pyplot as plt
from matplotlib import rc
rc('font',**{'family':'serif','serif':['Computer Modern Roman']})
rc('text', usetex=True)
import matplotlib.font_manager
import os
fm = matplotlib.font_manager.json_load(os.path.expanduser("~/.cache/matplotlib/fontlist-v300.json"))
fm.findfont("serif", rebuild_if_missing=False)
/home/david/.pyenv/versions/3.7.1/envs/jupyter-3.7.1/lib/python3.7/site-packages/matplotlib/font_manager.py:1241: UserWarning: findfont: Font family ['serif'] not found. Falling back to DejaVu Sans.
(prop.get_family(), self.defaultFamily[fontext]))
'/home/david/.pyenv/versions/3.7.1/envs/jupyter-3.7.1/lib/python3.7/site-packages/matplotlib/mpl-data/fonts/ttf/DejaVuSans.ttf'
/home/david/.pyenv/versions/3.7.1/envs/jupyter-3.7.1/lib/python3.7/site-packages/matplotlib/font_manager.py:1241: UserWarning: findfont: Font family ['serif'] not found. Falling back to Helvetica.
(prop.get_family(), self.defaultFamily[fontext]))
'/home/david/.pyenv/versions/3.7.1/envs/jupyter-3.7.1/lib/python3.7/site-packages/matplotlib/mpl-data/fonts/pdfcorefonts/Helvetica.afm'
2nd (and subsequent) run:
import matplotlib.pyplot as plt
from matplotlib import rc
rc('font',**{'family':'serif','serif':['Computer Modern Roman']})
rc('text', usetex=True)
import matplotlib.font_manager
import os
fm = matplotlib.font_manager.json_load(os.path.expanduser("~/.cache/matplotlib/fontlist-v300.json"))
fm.findfont("serif", rebuild_if_missing=False)
PS System python is 3.6, I'm running pyenv using python 3.7.1 and a virtualenv (called jupyter-3.7.1) if that's of any relevance. In particular it seems that the fonts are stored in a per virtualenv folder but the cache is shared across all python environments on the machine which could potentially cause issues - I'm only using the one pyenv though.
In theory the cache should be reusable across venvs (as long as they all have mpl>=3 (not sure of the exact lower bound)).
When you say no warning on the 2nd+ run, do you mean after restarting the kernel in between? Otherwise that's normal, that's due to the fact that Python keeps track of already emitted warnings and does not emit duplicate warnings (by default).
I still don't understand why deleting and regenerating the cache helps (can you provide your matplotlibrc? possibly relevant).
However I see at least one "bug" (say, inaccuracy) in the text layouting code now: even when usetex is True, we try to get the font extent using ismath=False, and that's apparently what triggers the warning (because then you need the font, e.g. Computer Modern Roman in your case, to actually be findable by non-tex machinery :/)
The 2nd+ run has no restart of the kernel - note though it appears to have found the correct (non-fallback) font on the second run?
I think the deletion of the cache is a red herring. It’s just that the warning is only triggered once and the 2nd time round it finds the requested font (I think)?
On 11 Jan 2019, at 9:00 pm, Antony Lee ***@***.***> wrote:
In theory the cache should be reusable across venvs (as long as they all have mpl>=3 (not sure of the exact lower bound)).
When you say no warning on the 2nd+ run, do you mean after restarting the kernel in between? Otherwise that's normal, that's due to the fact that Python keeps track of already emitted warnings and does not emit duplicate warnings (by default).
I still don't understand why deleting and regenerating the cache helps (can you provide your matplotlibrc? possibly relevant).
However I see at least one "bug" (say, inaccuracy) in the text layouting code now: even when usetex is True, we try to get the font extent using ismath=False, and that's apparently what triggers the warning (because then you need the font, e.g. Computer Modern Roman in your case, to actually be findable by non-tex machinery :/)
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub, or mute the thread.
The 2nd+ run has no restart of the kernel - note though it appears to have found the correct (non-fallback) font on the second run?
No? It's still falling back to dejavusans (the afm case is irrelevant I think).
I think it's just that the warning doesn't get printed out a second time (I think?).
A full repro example would still be useful, if only so that this doesn't occur again (or perhaps we can make the configuration problem more obvious, etc.).
@jaweriaamjad it seems like you do not have Arial installed, so that seems like the most likely problem. (Arial is a Microsoft font and it looks from the paths like you are not using Windows? Helvetica is a good replacement, as in Arial is a "copy" of Helvetica.)
I have problem to display chinese, I solved the problem by following these steps:
Download SimHei.ttf font file from internet and save it to python directory, for me, it's /Users/<username>/venv_3_9/lib/python3.9/site-packages/matplotlib/mpl-data/fonts/ttf
Delete ~/.cache/matplotlib and ~/.matplotlib/*.json
Find and delete all metadata of Font file, such as xattr -l SimHei.ttf, xattr -d com.apple.quarantine SimHei.ttf
Set font in python script: import matplotlib.pyplot as plt, plt.rcParams['font.sans-serif'] = ['SimHei']
If it doesn't work , try to reload python environment: cd /Users/<username>/venv_3_9/bin && deactivate && source ./activate
Download SimHei.ttf font file from internet and save it to python directory, for me, it's /Users/<username>/venv_3_9/lib/python3.9/site-packages/matplotlib/mpl-data/fonts/ttf
There is no need to put fonts in a system/package directory like this. Matplotlib will read fonts from standard system paths.
Humble attempt here. I am using Ubuntu WSL on Windows and had the same issues anytime I tried to plot anything with matplotlib. I noticed that by doing the following imports the warnings finally disappear:
import matplotlib as mpl
import matplotlib.pyplot as plt
For me the solution was to add import matplotlib as mpl. Hope this can solve the issue!
Activity
anntzer commented on Jan 9, 2019
Can you please provide your ~/.cache/matplotlib/fontlist-v300.json before and after deleting ~/.cache/matplotlib (and reimporting matplotlib to regenerate the json)? Thanks.
david-waterworth commented on Jan 10, 2019
Quick update. Issue occurred again last night (running jupyter notebooks as batch jobs).
I deleted ~/.cache/matplotlib and the issue resolved. I also reimported matplotlib but it didn't regenerate the json?
I'll keep an eye on it.
anntzer commented on Jan 10, 2019
"import matplotlib.font_manager" should regenerate it.
david-waterworth commented on Jan 10, 2019
I had to do it outside jupyter - seems to be ignoring %load_ext autoreload
david-waterworth commented on Jan 10, 2019
I've attached the fontlist-v300.json files before and after deleting ~/.cache/matplotlib
fontlist-v300-after.zip
fontlist-v300-before.zip
anntzer commented on Jan 11, 2019
Thanks. The caches look fine and I don't see how these can trigger the warning for now :/
I guess you can try
fm = matplotlib.font_manager.json_load("/path/to/fontlist-v300.json") fm.findfont("serif", rebuild_if_missing=False) fm.findfont("serif", fontext="afm", rebuild_if_missing=False)when the warning happens. Do these trigger the warning (in a fresh session)? Can you, say, debug-print the scoring in _findfont_cached()?
david-waterworth commented on Jan 11, 2019
I've been playing with a minimal notebook - if I restart kernel and clear outputs, the first time I run the cells below I get the warnings, the 2nd time there's no warning and the 2nd font found is different.
i.e. first run through after restarting kernel:
import matplotlib.pyplot as plt from matplotlib import rc rc('font',**{'family':'serif','serif':['Computer Modern Roman']}) rc('text', usetex=True) import matplotlib.font_manager import os fm = matplotlib.font_manager.json_load(os.path.expanduser("~/.cache/matplotlib/fontlist-v300.json")) fm.findfont("serif", rebuild_if_missing=False)fm.findfont("serif", fontext="afm", rebuild_if_missing=False)2nd (and subsequent) run:
import matplotlib.pyplot as plt from matplotlib import rc rc('font',**{'family':'serif','serif':['Computer Modern Roman']}) rc('text', usetex=True) import matplotlib.font_manager import os fm = matplotlib.font_manager.json_load(os.path.expanduser("~/.cache/matplotlib/fontlist-v300.json")) fm.findfont("serif", rebuild_if_missing=False)fm.findfont("serif", fontext="afm", rebuild_if_missing=False)david-waterworth commented on Jan 11, 2019
PS System python is 3.6, I'm running pyenv using python 3.7.1 and a virtualenv (called jupyter-3.7.1) if that's of any relevance. In particular it seems that the fonts are stored in a per virtualenv folder but the cache is shared across all python environments on the machine which could potentially cause issues - I'm only using the one pyenv though.
anntzer commented on Jan 11, 2019
In theory the cache should be reusable across venvs (as long as they all have mpl>=3 (not sure of the exact lower bound)).
When you say no warning on the 2nd+ run, do you mean after restarting the kernel in between? Otherwise that's normal, that's due to the fact that Python keeps track of already emitted warnings and does not emit duplicate warnings (by default).
I still don't understand why deleting and regenerating the cache helps (can you provide your matplotlibrc? possibly relevant).
However I see at least one "bug" (say, inaccuracy) in the text layouting code now: even when usetex is True, we try to get the font extent using ismath=False, and that's apparently what triggers the warning (because then you need the font, e.g. Computer Modern Roman in your case, to actually be findable by non-tex machinery :/)
david-waterworth commented on Jan 11, 2019
anntzer commented on Jan 11, 2019
No? It's still falling back to dejavusans (the afm case is irrelevant I think).
I think it's just that the warning doesn't get printed out a second time (I think?).
david-waterworth commented on Jan 12, 2019
OK, the afm case did change though? Presumably it should be deterministic
run 1:
'/home/david/.pyenv/versions/3.7.1/envs/jupyter-3.7.1/lib/python3.7/site-packages/matplotlib/mpl-data/fonts/pdfcorefonts/Helvetica.afm'
run 2:
'/home/david/.pyenv/versions/3.7.1/envs/jupyter-3.7.1/lib/python3.7/site-packages/matplotlib/mpl-data/fonts/afm/phvr8a.afm'
anntzer commented on Jan 12, 2019
Perhaps it should, but it's not clear it is... (both of these fonts are Helvetica)
david-waterworth commented on Jan 13, 2019
I missed your request to provide my matplotlibrc.zip
matplotlib.matplotlib_fname()
'/home/david/.pyenv/versions/3.7.1/envs/jupyter-3.7.1/lib/python3.7/site-packages/matplotlib/mpl-da
I've not customised it, there's nothing in $HOME/.config/matplotlib
33 remaining items
anntzer commented on Mar 21, 2022
A full repro example would still be useful, if only so that this doesn't occur again (or perhaps we can make the configuration problem more obvious, etc.).
DBerke commented on Mar 21, 2022
Ok, I'll try to get one this weekend, I'm pressed for time this week.
anntzer commented on Mar 21, 2022
No hurries.
sakshamg94 commented on Mar 23, 2022
Same issue. I tried both the solutions proposed here:
Neither work for me
hkayann commented on Mar 24, 2022
@sakshamg94 can you try the steps I provided on SO.
iDTer commented on Apr 28, 2022
didn't help...
iDTer commented on Apr 28, 2022
Why this bug remains unsolved to this day....
lancelot1969 commented on May 4, 2022
Having the same issue but in my case:
findfont: Font family ['Heiti TC'] not found. Falling back to DejaVu Sans.
GorrepatiKaushik commented on May 4, 2022
I have the same issue
findfont: Font family ['century-gothic'] not found. Falling back to DejaVu Sans.
hoochanlon commented on Jun 25, 2023
Maybe.
jaweriaamjad commented on Nov 7, 2023
I am having a similar issue. I have been trying to set fonts as follows:
matplotlib.rcParams['font.family'] = "sans-serif" matplotlib.rcParams['font.sans-serif'] = "Arial"
I have also tried with matplotli.pyplot but keep getting the following message:
findfont: Generic family 'sans-serif' not found because none of the following families were found: Arial
I have looked at the fonts cache and apparently there are not Arial fonts. I have tried removing the cache and regenerating it but the error persists.
fontlist-v300.zip
oscargus commented on Nov 7, 2023
@jaweriaamjad it seems like you do not have Arial installed, so that seems like the most likely problem. (Arial is a Microsoft font and it looks from the paths like you are not using Windows? Helvetica is a good replacement, as in Arial is a "copy" of Helvetica.)
ZPVIP commented on Mar 9, 2024
I have problem to display chinese, I solved the problem by following these steps:
If it doesn't work , try to reload python environment: cd /Users/<username>/venv_3_9/bin && deactivate && source ./activate
QuLogic commented on Mar 9, 2024
There is no need to put fonts in a system/package directory like this. Matplotlib will read fonts from standard system paths.
locorico25 commented on Apr 2, 2024
Humble attempt here. I am using Ubuntu WSL on Windows and had the same issues anytime I tried to plot anything with matplotlib. I noticed that by doing the following imports the warnings finally disappear:
import matplotlib as mpl
import matplotlib.pyplot as plt
For me the solution was to add import matplotlib as mpl. Hope this can solve the issue!