Matplotlib cannot find basic fonts

后端 未结 10 708
一生所求
一生所求 2020-12-08 00:34

I am using matplotlib version 2.0.0 on Python 3 in a miniconda virtual environment. I am working on a unix scientific computing cluster where I don\'t have root privileges.

相关标签:
10条回答
  • 2020-12-08 00:53

    There is a conda package for it[1]. So, you don't really need sudo to fix this!

    conda install -c conda-forge -y mscorefonts
    

    restart jupyter and/or force rebuild matplotlib font cache

    import matplotlib
    matplotlib.font_manager._rebuild()
    

    [1] - https://anaconda.org/conda-forge/mscorefonts

    0 讨论(0)
  • 2020-12-08 00:55

    I am in macOS with jupyter notebook, I solved with the following, first close your jupyter notebook. Then find out the font path by doing the following in Python

    import matplotlib
    print(matplotlib.matplotlib_fname())
    

    it prints /Users/zyy/anaconda2/lib/python2.7/site-packages/matplotlib/mpl-data/matplotlibrc for me, notice matplotlibrc is a file, not a directory.

    Then download font SimHei, and copy it to the directory fonts/ttf under the mpl-data/ directory above.

    Delete directory ~/.cache/matplotlib and restart your jupyter notebook, everything should be good.

    0 讨论(0)
  • 2020-12-08 00:58

    A solution for Windows users, when confronted with the warning:

    UserWarning: findfont: Font family ['serif'] not found. Falling back to DejaVu Sans
    (prop.get_family(), self.defaultFamily[fontext]))
    
    1. Delete the fonts located in matplotlib's cache.
      Cache's location: import matplotlib as mpl; print(mpl.font_manager.get_cachedir())

    2. Find matplotlib's font directory. The path might be similar to
      C:\Miniconda3\pkgs\matplotlib-2.2.2-py36_1\Lib\site-packages\matplotlib\mpl-data\fonts\ttf

    3. Copy necessary fonts like Computer Modern to this directory.

    The warning may persist, but your plots' font should change appropriately.

    0 讨论(0)
  • 2020-12-08 01:04

    This work for me::

    $ sudo apt-get install msttcorefonts -qq
    
    0 讨论(0)
提交回复
热议问题