any plot with the xkcd style pyplot.xkcd() generates a bunch of errors from the findfont function under Windows even when fonts like "Comic Neue" are installed. I also regenerated the font cache and copied the font files into the matplotlib site-packages font folder, but this did not help. (I am using mpl 3.8.2 on python 3.11)
I am unsure if this is an issue under other OSs but I have the same issue under Win10 and Win11
Proposed fix
In the end I modified the pyplot.py file in the xkcd function, to get rid of the errors
I think this is older than that; the caching on the warning seems either very specific, or non-existent, as it seems to trigger at all draws (e.g., pan or resize, and a bunch of warnings are printed.)
implicitly assuming that all entries are family names. If this list also contains concrete font names, a warning will be issued if a font name is not found. Prior to v3.6.0, only the existing font files from the font cache were reviewed in _findfont_cached.
See the following example:
import matplotlib as mpl
import matplotlib.pyplot as plt
with mpl.rc_context({'font.family': ["monospace", "DejaVu Sans Mono", "Bitstream Vera Sans Mono"]}):
fig = plt.figure()
fig.text(0.4, 0.5, "mimi\nimim", size=40)
Starting with v3.6.0 it returns the warnings WARNING:matplotlib.font_manager:findfont: Font family 'Bitstream Vera Sans Mono' not found.. No warnings occurred up to v3.5.3.
So to fix this concrete issue it would indeed be sufficient to set font.family to 'sans-serif' (just in case it has been changed from the default before) and then set the list of concrete fonts to 'font.sans-serif' as @bkasel already found out. This doesn't address, however, the underlying root cause.
If this list also contains concrete font names, a warning will be issued if a font name is not found.
My preference would be to allow font-names as part of finding family names (or control w/ a flag that defaults to permissive), what am I missing for why that's a bad idea?
My preference would be to allow font-names as part of finding family names (...), what am I missing for why that's a bad idea?
I didn't say that font.family must not contain concrete font names (sorry if I phrased that in a misleading way). On the contrary, I think it might be sufficient to just emit the warning only for font families, not for concrete fonts (but I didn't check yet if this completely restores the old behavior):
@@ -1367,7 +1367,7 @@ class FontManager:
"none of the following families were found: %s",
family, ", ".join(self._expand_aliases(family))
)
- else:
+ elif family in ["serif", "sans-serif", "cursive", "fantasy", "monospace"]:
_log.warning("findfont: Font family %r not found.", family)
I guess it should read "if the font doesn't exist". If the font exists then findfont will not raise and no warning will be issued. But I think that we're both talking about the same thing.
But I think that we're both talking about the same thing.
Probably - what I'm getting at is basically that when it comes to not found error messages, we should be assuming something like this relation for the rcparams: $font.[serif, fantasy, etc] \subset font.family$
so that if updating $font.sans-serif$ doesn't raise a warning, than updating $font.family$ also shouldn't. I know technically $family \neq font$ but there are lots of places in our examples were we pass in a font to a family parameter so we're kinda using them interchangeably anyway.
Activity
story645 commented on Nov 20, 2023
I'd assume this would be due to #27339 but the only change there was removing humor sans.
QuLogic commented on Nov 21, 2023
I think this is older than that; the caching on the warning seems either very specific, or non-existent, as it seems to trigger at all draws (e.g., pan or resize, and a bunch of warnings are printed.)
story645 commented on Nov 21, 2023
That's what I thought was going on w/ the findfont warning - is why I started #26602
Also I don't know if it's a windows specific thing, but I think I have to delete my fontcache to force a rebuild.
StefRe commented on Nov 23, 2023
These warnings are caused by _find_fonts_by_props introduced in #20740 with matplotlib 3.6.0.
This function searches the complete list of font.family:
matplotlib/lib/matplotlib/font_manager.py
Line 1351 in 0a4efbe
implicitly assuming that all entries are family names. If this list also contains concrete font names, a warning will be issued if a font name is not found. Prior to v3.6.0, only the existing font files from the font cache were reviewed in _findfont_cached.
See the following example:
import matplotlib as mpl import matplotlib.pyplot as plt with mpl.rc_context({'font.family': ["monospace", "DejaVu Sans Mono", "Bitstream Vera Sans Mono"]}): fig = plt.figure() fig.text(0.4, 0.5, "mimi\nimim", size=40)Starting with v3.6.0 it returns the warnings WARNING:matplotlib.font_manager:findfont: Font family 'Bitstream Vera Sans Mono' not found.. No warnings occurred up to v3.5.3.
So to fix this concrete issue it would indeed be sufficient to set font.family to 'sans-serif' (just in case it has been changed from the default before) and then set the list of concrete fonts to 'font.sans-serif' as @bkasel already found out. This doesn't address, however, the underlying root cause.
story645 commented on Nov 24, 2023
My preference would be to allow font-names as part of finding family names (or control w/ a flag that defaults to permissive), what am I missing for why that's a bad idea?
StefRe commented on Nov 24, 2023
I didn't say that font.family must not contain concrete font names (sorry if I phrased that in a misleading way). On the contrary, I think it might be sufficient to just emit the warning only for font families, not for concrete fonts (but I didn't check yet if this completely restores the old behavior):
@@ -1367,7 +1367,7 @@ class FontManager: "none of the following families were found: %s", family, ", ".join(self._expand_aliases(family)) ) - else: + elif family in ["serif", "sans-serif", "cursive", "fantasy", "monospace"]: _log.warning("findfont: Font family %r not found.", family)story645 commented on Nov 24, 2023
I think I'm the one who phrased things weird, I was just asking if we could expand find font family to not raise a warning if the font exists
StefRe commented on Nov 24, 2023
I guess it should read "if the font doesn't exist". If the font exists then findfont will not raise and no warning will be issued. But I think that we're both talking about the same thing.
story645 commented on Nov 24, 2023
Probably - what I'm getting at is basically that when it comes to not found error messages, we should be assuming something like this relation for the rcparams: $font.[serif, fantasy, etc] \subset font.family$
so that if updating $font.sans-serif$ doesn't raise a warning, than updating $font.family$ also shouldn't. I know technically $family \neq font$ but there are lots of places in our examples were we pass in a font to a family parameter so we're kinda using them interchangeably anyway.