解决linux服务器上matplotlib中文显示乱码问题

报错信息: UserWarning: findfont: Font family [u'sans-serif'] not found. Falling back to DejaVu Sans

找不到字体,在绘制的图片中中文显示乱码

解决方法:

1. 查看配置路径:

import matplotlib 
print (matplotlib.matplotlib_fname())

2. 下载字体:simhei.tff : http://www.font5.com.cn/font_download.php?id=151&part=1237887120

3. 将字体拷贝到 tff 目录下:

/home/hwy/anaconda3/lib/python3.7/site-packages/matplotlib/mpl-data/fonts/tff/

5. 删除字体缓存文件:

首先找到字体缓存文件的位置:

import matplotlib as mpl
fm = mpl.font_manager
fm.get_cachedir()

这是我缓存文件的位置:

/home/hwy/.cache/matplotlib/

删掉就好了:

rm -rf /home/hwy/.cache/matplotlib/

6. 字体参数设置:

修改配置文件 matplotlibrc:

将一下三句修改,并将 # 去掉:

font.family         : sans-serif 
...
font.sans-serif     : SimHei
...
axes.unicode_minus  : False # use unicode for the minus symbol

意思就是知道字库族为 sans-serif,同时添加“SimHei”即宋体到字库族列表中,同时将找到axes.unicode_minus,将True改为False,作用就是解决负号’-‘显示为方块的问题。

成功显示中文了!!

下面的博客配置的蛮好的。

http://xiaqunfeng.cc/2018/03/13/mac%E4%B8%8AMatplotlib%E4%B8%AD%E6%96%87%E4%B9%B1%E7%A0%81%E9%97%AE%E9%A2%98/

猜你喜欢

转载自www.cnblogs.com/Bella2017/p/10959231.html