网上教程很多,但是折腾了好久才弄好。特此记录一下。
查看matplotlib配置文件位置
>>> import matplotlib
>>> print matplotlib.matplotlib_fname()
/home/ubuntu/anaconda3/lib/python3.6/site-packages/matplotlib/mpl-data/matplotlibrc
PS:因为之前安装了两个matplotlib的库。所以这里会有问题。
一个使用sudo apt-get install python-matplotlib安装,在root用户下。
一个使用pip install matplotlib普通用户安装。
后来使用 sudo apt-get remove python-matplotlib后,才能显示正常的matplotlib配置文件。
永久解决方法
首先将windwos中fonts目录下的simhei.ttf拷贝到/home/ubuntu/anaconda3/pkgs/matplotlib-2.2.2-py36h0e671d2_1/lib/python3.6/site-packages/matplotlib/mpl-data/fonts/ttf/目录中
修改配置文件
去除这三列之前的‘#’
在font.sans-serif后添加,simhei
```
#font.family : sans-serif
#font.sans-serif : simhei, Bitstream Vera Sans, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif
...
#axes.unicode_minus : False #解决负号'-'显示为方块的问题
```
删除~/.cache/matplotlib的缓冲
验证
新建python文件,输入以下内容
# -*- coding:utf-8 -*-
from matplotlib import pyplot as plt
plt.title(u'显示中文')
plt.show()
若出现如下图的中文字符,则说明更改成功。
最后附上simhei.ttf的链接
simhei.ttf
参考:
https://blog.csdn.net/github_33934628/article/details/77874674