在linux新的python环境,安装了matplotlib,但是使用中文字体时找不到。
问题代码:
import matplotlib.pyplot as plt plt.rcParams['font.sans-serif'] = ['SimHei'] # 中文字体设置
报错:
findfont: Font family ['sans-serif'] not found. Falling back to DejaVu Sans. findfont: Generic family 'sans-serif' not found because none of the following families were found: SimHei解决
首先,下载需要的字体。
然后将字体放到 /site-packages/matplotlib/mpl-data/fonts/ttf下。
找到python字体缓存文件:
import matplotlib print(matplotlib.get_cachedir())
/root/.cache/matplotlib
删除缓存:
rm -rf /root/.cache/matplotlib/*
重新运行即可。


![linux下python matplotlib 中文字体Font family [‘sans-serif‘] not found. Falling back to DejaVu Sans linux下python matplotlib 中文字体Font family [‘sans-serif‘] not found. Falling back to DejaVu Sans](http://www.mshxw.com/aiimages/31/829370.png)
