查找matplotlib字体安装路径

import matplotlib
print(matplotlib.matplotlib_fname())

下载simhei.tff字体

下载’SimHei’
将字体保存在上述路径中mpl-data/fonts/tff的路径中

打开第一步中得到的路径中matplotlibrc文件,将下述两行前的#去掉, 并在第二行加入SimHei字体

#font.family : sans-serif
#font.serif : SimHei, DejaVu Serif, Bitstream Vera Serif, Computer Modern Roman, New

清除字体缓存

cd ~/.matplotlib/
rm -rf ~/.matplotlib/*.cache
rm -rf fontList.py3k.cache

在代码中加入下面两行

plt.rcParams[‘font.sans-serif’] = [‘SimHei’] # 用来正常显示中文标签
plt.rcParams[‘axes.unicode_minus’] = False # 用来正常显示负号

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐