Skip to content

[Bug]: Warning: Font Family not found #20850

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
FranzForstmayr opened this issue Aug 17, 2021 · 6 comments · May be fixed by #22111
Open

[Bug]: Warning: Font Family not found #20850

FranzForstmayr opened this issue Aug 17, 2021 · 6 comments · May be fixed by #22111

Comments

@FranzForstmayr
Copy link

Bug summary

Usign Latex fonts as recommended here throws a Warning:

findfont: Font family ['serif'] not found. Falling back to DejaVu Sans.
findfont: Generic family 'serif' not found because none of the following families were found: 

Code for reproduction

import matplotlib.pyplot as plt

plt.rcParams.update({
    "font.family": "serif",
    # Use LaTeX default serif font.
    "font.serif": [],
    # Use specific cursive fonts.
    "font.cursive": ["Comic Neue", "Comic Sans MS"],
})

plt.figure()
plt.plot([1,2,3,4])
plt.savefig('test.png')
plt.savefig('test.pgf')

Actual outcome

The Bug is also visible in your Docs.
https://matplotlib.org/stable/gallery/userdemo/pgf_fonts.html

Expected outcome

No Warning should get thrown everytime

Operating system

Ubuntu

Matplotlib Version

3.4.2

Matplotlib Backend

TkAgg

Python version

3.9.5

Jupyter version

No response

Other libraries

No response

Installation

pip

Conda channel

No response

@FranzForstmayr
Copy link
Author

Similar to #13139

@aitikgupta
Copy link
Contributor

Adding documentation tag since the example itself is slightly incorrect..
When you did:

    "font.family": "serif",
    "font.serif": [],

It's really overriding the default Matplotlib serif font-family as an empty list; whereas it really should've been untouched (i.e., the defaults mentioned at: https://matplotlib.org/stable/tutorials/text/usetex.html#sphx-glr-tutorials-text-usetex-py)

I'm not sure why the example was designed in this way.. if the fonts were available you should've got a PGF/PDF with these fonts:

pdffonts pgf_fonts.pdf                                                                                             
name                                 type              encoding         emb sub uni object ID
------------------------------------ ----------------- ---------------- --- --- --- ---------
BXGEVL+DejaVuSerif                   Type 3            Custom           yes yes no      52  0
GCWXDV+DejaVuSans-Oblique            Type 3            Custom           yes yes no      23  0
FJGZKM+DejaVuSansMono                Type 3            Custom           yes yes no      40  0
BMQQDV+DejaVuSans                    Type 3            Custom           yes yes no      28  0
TVXSBT+ComicNeue-Regular             Type 3            Custom           yes yes no      15  0

However, with the current example; serif font-family isn't found (because the list is essentially empty!); this is what you get:

$ pdffonts pgf_fonts.pdf               
name                                 type              encoding         emb sub uni object ID
------------------------------------ ----------------- ---------------- --- --- --- ---------
BMQQDV+DejaVuSans                    Type 3            Custom           yes yes no      28  0
GCWXDV+DejaVuSans-Oblique            Type 3            Custom           yes yes no      23  0
FJGZKM+DejaVuSansMono                Type 3            Custom           yes yes no      51  0
TVXSBT+ComicNeue-Regular             Type 3            Custom           yes yes no      15  0

Note the missing serif font...

Fix is pretty simple, something like:

 import matplotlib.pyplot as plt
 plt.rcParams.update({
+    # Use LaTeX default serif font-family.
+    # https://matplotlib.org/stable/tutorials/text/usetex.html#sphx-glr-tutorials-text-usetex-py
     "font.family": "serif",
-    # Use LaTeX default serif font.
-    "font.serif": [],
     # Use specific cursive fonts.
     "font.cursive": ["Comic Neue", "Comic Sans MS"],
 })

You're welcome to create a PR :)

@aitikgupta aitikgupta added the Good first issue Open a pull request against these issues if there are no active ones! label Aug 29, 2021
@FranzForstmayr
Copy link
Author

I'll go for it

FranzForstmayr added a commit to FranzForstmayr/matplotlib that referenced this issue Aug 30, 2021
Do not remove serif font family in pgf example to avoid Warning
@urrameu
Copy link

urrameu commented Sep 6, 2021

Adding documentation tag since the example itself is slightly incorrect.
[...]
It's really overriding the default Matplotlib serif font-family as an empty list; whereas it really should've been untouched

The empy list is suggested here: https://matplotlib.org/stable/tutorials/text/pgf.html (section "Font specification"). So, either this is correct or that section of the documentation should also be modified. In any case,

FranzForstmayr added a commit to FranzForstmayr/matplotlib that referenced this issue Sep 27, 2021
This reverts commit 0bff071.
FranzForstmayr added a commit to FranzForstmayr/matplotlib that referenced this issue Nov 1, 2021
Do not remove serif font family in pgf example to avoid Warning

Add requested changes

Revert "Fix matplotlib#20850"

This reverts commit 0bff071.
@oscargus
Copy link
Member

oscargus commented Jan 5, 2022

So, either this is correct or that section of the documentation should also be modified.

That seems to be correct if the pgf backend is used. However, in https://matplotlib.org/stable/gallery/userdemo/pgf_fonts.html pdf is not used. Adding

import matplotlib as mpl
mpl.use("pgf")

first in the example file may help. (I'll add a PR.)

@oscargus
Copy link
Member

oscargus commented Jan 5, 2022

Wasn't that easy...

The sentence "You can also use the LaTeX default Computer Modern fonts by clearing the lists for rcParams["font.serif"]" is currently not correct since the code:

if texcommand != "pdflatex" and mpl.rcParams["pgf.rcfonts"]:
families = ["serif", "sans\\-serif", "monospace"]
commands = ["setmainfont", "setsansfont", "setmonofont"]
for family, command in zip(families, commands):
# 1) Forward slashes also work on Windows, so don't mess with
# backslashes. 2) The dirname needs to include a separator.
path = pathlib.Path(fm.findfont(family))
latex_fontspec.append(r"\%s{%s}[Path=\detokenize{%s}]" % (
command, path.name, path.parent.as_posix() + "/"))

will use the default Matplotlib font instead.

(Unless pdflatex is used, in which case the fonts are not set.)

So, I've changed this to

                path = pathlib.Path(fm.findfont(family),
                                    fallback_to_default=False)

which then will not set it to the default font. Not sure if it will break anything else.

@melissawm melissawm removed the Good first issue Open a pull request against these issues if there are no active ones! label May 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment