linux乱码问题解决

一、解决Linux乱码问题

  1. 问题出现在运行matplotlib图片的title乱码
    2.步骤
    1. 导入字体
      • 网上下载
      • 从C:/Windows/fonts里面获取导入
    2. 以黑体SimHei.ttf为例
1. mkdir /usr/share/fonts/chinese           # 创建一个文件夹
2. cp SimHei.ttf /usr/share/fonts/chinese/
3. mkfontscale                              # 添加目录和字体到配置文件,如果command not found 用yum -y install mkfontscale
4. mkfontdir
5. fc-cache                                 # 刷新字体缓存
6. fc-list :lang=zh                         # 查看中文字体

二、matplotlib乱码问题

1. find |grep matplot|grep fonts             # 在根目录下 查找matplotlib内置的fonts目录
2. cp  SimHei.ttf /usr/local/lib64/python3.6/site-packages/matplotlib/mpl-data/fonts/ttf/ #将字体也复制给matplotlib一份
3. vim /usr/local/lib64/python3.6/site-packages/matplotlib/mpl-data/matplotlibrc
4. 去掉font.family、font.sans-serif的注释,并在sans最后加入你的字体的名称,eg:SimHei,同时令axes.unicode_minus = False ,确保中文减号正常显示
5. rm -rf ~/.cache/matplotlib/*              # 清除matplotlib的字体缓存
6. 若是使用jupyter,重启当前的运行的项目,重新执行代码就可以看到方块已经改为中文了

谁不是孤身一人,翻山越岭
原文地址:https://www.cnblogs.com/hasz/p/13452897.html