9.1 Linux环境下Jupyter中matplotlib中文乱码¶
解决方法:
下载SimHei字体(百度搜一个就可以)
找到matplotlib的文件位置
import matplotlib
matplotlib.matplotlib_fname()
查看到matplotlib的文件路径,我的路径是:
/apps/home/rd/anaconda3/lib/python3.7/site-packages/matplotlib/mpl-data/matplotlibrc
进入到mpl-data/fonts路径,把下载好的SimHei.ttf文件导入进去
返回到
mpl-data目录,打开matplotlibrc文件,搜索定位到font.family取消font.family、font.serif和font.sans-serif的注释,并在font.serif和font.sans-serif后添加SimHei,保存进入到
~/.cache目录,删除该目录下的matplotlib文件夹
cd ~/.cache
rm -rf matplotlib
重新打开jupyter,可以输入如下代码查看是否导入SimHei字体
from matplotlib import font_manager
a = sorted([f.name for f in font_manager.fontManager.ttflist])
for i in a:
print(i)
在导入matplotlib包之后加一句
plt.rcParams['font.sans-serif']=['SimHei']