您的位置:首页 > 编程语言 > PHP开发

matplotlib 与 seaborn 中出现中文乱码的解决方法

2017-11-06 14:49 661 查看
Linux、Mac osx 系统中,出现 matplotlib 或 seaborn 绘图中有中文乱码的情形,可以考虑使用以下方式处理:

到 anaconda 的 matplotlib 中查看是否有 simhei.ttf 字体:

cd ~/anaconda3/lib/python3.5/site-packages/matplotlib/mpl-data/fonts/ttf
ls -al | grep simhei


如果没有,从 windows 中用 everything 搜索全局文件,找到 simhei.ttf,并将其上传到linux 的 matplotlib 的 fonts/ttf 文件夹

修改配置文件~/anaconda3/lib/python3.5/site-packages/matplotlib/mpl-data/matplotlibrc 文件,

将该文件拷贝到.cache/matplotlib 目录下,并找到以下两行,改为如下:

  

font.family         : sans-serif
font.sans-serif     : simhei, Bitstream Vera Sans, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif


(linux)删除~/.cache/matplotlib/目录下的 fonts cache list

代码设置 matplotlib 和 seaborn 的环境
  

import matplotlib as mpl
mpl.rcParams['font.sans-serif'] = ['simhei']
mpl.rcParams['font.serif'] = ['simhei']
import seaborn as sns
sns.set_style("darkgrid",{"font.sans-serif":['simhei','Droid Sans Fallback']})


参考:

https://github.com/mwaskom/seaborn/issues/1009

http://www.th7.cn/Program/Python/201704/1156781.shtml
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: