Matplotlib can't find font

后端 未结 3 1031
我寻月下人不归
我寻月下人不归 2021-01-04 19:27

I am trying to draw an xkcd-style plot with matplotlib (ver. 1.4.2) under Python 3.

When I try to run:

import matplotlib.pyplot as plt
plt.xkcd()
plt         


        
相关标签:
3条回答
  • 2021-01-04 20:02

    If you add a new font after installing matplotlib then try to remove the font cache. Matplotlib will have to rebuild the cache, thereby adding the new font.

    It may be located under ~/.matplotlib/fontList.cache or ~/.cache/matplotlib/fontList.json.

    0 讨论(0)
  • 2021-01-04 20:08

    Just in case somebody wants to choose a custom font for their chart. You can manually set up the font for your chart labels, title, legend, or tick labels. The following code demonstrates how to set a custom font for your chart. And the error you mentioned can disappear.

    import matplotlib.font_manager as fm
    import matplotlib.pyplot as plt
    
    font_path = '/System/Library/Fonts/PingFang.ttc'  # the location of the font file
    my_font = fm.FontProperties(fname=font_path)  # get the font based on the font_path
    
    fig, ax = plt.subplots()
    
    ax.bar(x, y, color='green')
    ax.set_xlabel(u'Some text', fontproperties=my_font)
    ax.set_ylabel(u'Some text', fontproperties=my_font)
    ax.set_title(u'title', fontproperties=my_font)
    for label in ax.get_xticklabels():
        label.set_fontproperties(my_font)
    
    0 讨论(0)
  • 2021-01-04 20:11

    For Mac User: try to run this command in python: (or before the .py file)

    import matplotlib
    
    matplotlib.font_manager._rebuild()
    
    0 讨论(0)
提交回复
热议问题