首页 > 技术文章 > 解决在Ubuntu系统下用matplotlib作图时出现中文乱码问题

fingerling 2018-01-24 16:19 原文

今天在jupyter notebook中画图时,中文不能正常显示,如下图所示:

在此记录一下解决的办法。

1.找到matplotlib配置文件的位置

import matplotlib
print(matplotlib.matplotlib_fname())

/usr/local/anaconda3/lib/python3.5/site-packages/matplotlib/mpl-data/matplotlibrc

2.修改配置文件matplotlibrc:

font.family         : sans-serif

font.sans-serif     : SimHei, DejaVu Sans, Bitstream Vera Sans, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif(在font.sans-serif后添加SimHei,用来正常显示中文标签)

axes.unicode_minus  : False(把True改为False,用来正常显示负号)

3.下载SimHei.ttf

4.将下载的SimHei.ttf文件放到/usr/local/anaconda3/lib/python3.5/site-packages/matplotlib/mpl-data/fonts/ttf目录下(注意修改此文件的权限)

5.删除~/.cache/matplotlib目录下的缓存

6.重新画图,显式正常:

 

推荐阅读