FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

font family ['serif'] not found. Falling back to DejaVu Sans · Issue #13139 · matplotlib/matplotlib · GitHub

Repository navigation

font family ['serif'] not found. Falling back to DejaVu Sans #13139

Closed

Description

Bug report

Bug summary

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

Matplotlib version

  • Operating system: Ubuntu 18.04
  • Matplotlib version: 3.0.2
  • Matplotlib backend: module://ipykernel.pylab.backend_inline
  • Python version: 3.7.1 x64
  • Jupyter version (if applicable): 4.4.0
  • Other libraries:

installed from pip

Activity

  1. anntzer commented on Jan 9, 2019

    Contributor

    Can you please provide your ~/.cache/matplotlib/fontlist-v300.json before and after deleting ~/.cache/matplotlib (and reimporting matplotlib to regenerate the json)? Thanks.

  2. david-waterworth commented on Jan 10, 2019

    Author

    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.

  3. anntzer commented on Jan 10, 2019

    Contributor

    "import matplotlib.font_manager" should regenerate it.

  4. david-waterworth commented on Jan 10, 2019

    Author

    I had to do it outside jupyter - seems to be ignoring %load_ext autoreload

  5. david-waterworth commented on Jan 10, 2019

    Author

    I've attached the fontlist-v300.json files before and after deleting ~/.cache/matplotlib

    fontlist-v300-after.zip
    fontlist-v300-before.zip

  6. anntzer commented on Jan 11, 2019

    Contributor

    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()?

  7. david-waterworth commented on Jan 11, 2019

    Author

    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'

    fm.findfont("serif", fontext="afm", 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 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)
    

    '/home/david/.pyenv/versions/3.7.1/envs/jupyter-3.7.1/lib/python3.7/site-packages/matplotlib/mpl-data/fonts/ttf/DejaVuSans.ttf'

    fm.findfont("serif", fontext="afm", rebuild_if_missing=False)
    

    '/home/david/.pyenv/versions/3.7.1/envs/jupyter-3.7.1/lib/python3.7/site-packages/matplotlib/mpl-data/fonts/afm/phvr8a.afm'

  8. david-waterworth commented on Jan 11, 2019

    Author

    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.

  9. anntzer commented on Jan 11, 2019

    Contributor

    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 :/)

  10. david-waterworth commented on Jan 11, 2019

    Author
  11. anntzer commented on Jan 11, 2019

    Contributor

    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?).

  12. david-waterworth commented on Jan 12, 2019

    Author

    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'

  13. anntzer commented on Jan 12, 2019

    Contributor

    Perhaps it should, but it's not clear it is... (both of these fonts are Helvetica)

  14. david-waterworth commented on Jan 13, 2019

    Author

    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

  15. 33 remaining items

  16. anntzer commented on Mar 21, 2022

    Contributor

    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.).

  17. DBerke commented on Mar 21, 2022

    Ok, I'll try to get one this weekend, I'm pressed for time this week.

  18. anntzer commented on Mar 21, 2022

    Contributor

    No hurries.

  19. sakshamg94 commented on Mar 23, 2022

    Same issue. I tried both the solutions proposed here:

    • matplotlib.font_manager._rebuild()
    • removing everything in ~/.cache/matplotlib/ and then restarting Jupyter notebook.

    Neither work for me

  20. hkayann commented on Mar 24, 2022

    @sakshamg94 can you try the steps I provided on SO.

  21. iDTer commented on Apr 28, 2022

    @sakshamg94 can you try the steps I provided on SO.

    didn't help...

  22. iDTer commented on Apr 28, 2022

    Why this bug remains unsolved to this day....

  23. 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.

  24. GorrepatiKaushik commented on May 4, 2022

    I have the same issue
    findfont: Font family ['century-gothic'] not found. Falling back to DejaVu Sans.

  25. hoochanlon commented on Jun 25, 2023

    Maybe.

    import platform
    import os
    
    system = platform.system()
    
    if system == 'Darwin':  # macOS
      font_path = '/System/Library/Fonts/STHeiti Light.ttc'
    elif system == 'Windows':
      font_path = 'C:/Windows/Fonts/simhei.ttf'
    else:  #  unix
      font_path = 'simhei.ttf'
    
    custom_font = fm.FontProperties(fname=font_path)
  26. 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

  27. oscargus commented on Nov 7, 2023

    Member

    @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.)

  28. ZPVIP commented on Mar 9, 2024

    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

  29. QuLogic commented on Mar 9, 2024

    Member

    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.

  30. 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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions


      Back | FazBrowse Home | New Git URL