一.下载中文字体(https://pan.baidu.com/s/1EqabwENMxR2WJrHfKvyrIw 这里下载多是SImhei字体)
安装字体:
解压:unzip SimHei.zip
拷贝字体到 usr/share/fonts 下(记得切换到.ttf的目录下面)
sudo cp ./SimHei.ttf /usr/share/fonts/SimHei.ttf
二. 查找matplotlib的配置目录
python
import matplotlib
print(matplotlib.matplotlib_fname())
结果如下图所示

三:更改matplotlib配置
sudo gedit /home/huster/anaconda3/lib/python3.6/site-packages/matplotlib/mpl-data/matplotlibrc
重点修以下三个地方(对应多注释#要去掉):
1.font.family : sans-serif
2.font.sans-serif: 这个字段要加上下载的中文字体名字,笔者使用多是Simhei,那么在后面字段的段首加上 Simhei
3.axes.unicode_minus : False
四:删除matplotlib字体缓存
python
import matplotlib
print(matplotlib.get_cachedir())
找到对应对应的缓存目录,然后rm该目录下的fontList.json文件
sudo rm /home/huster/.cache/matplotlib/fontList.json
本文详细介绍了如何在Matplotlib中正确显示中文,包括下载并安装中文字体、配置Matplotlib的相关参数、清除字体缓存等步骤。

1143

被折叠的 条评论
为什么被折叠?



