zoukankan      html  css  js  c++  java
  • Linux(deepin) 系统: 解决 matplotlib 中文乱码问题

    一、问题:

    matplotlib 库设置中文显示时会出现小方框,为了解决这个问题查阅了很多资料,和Ubuntu,centos等Linux系统解决方法不一样,按照查询方法无法解决问题,因此查了很久之后找到了解决方法。

     二、解决方法

    1. 下载字体
      下载中文字体 SimHei.ttf

    2. 删除当前用户 matplotlib 的缓冲文件

    # 终端执行下面命令
    cd ~/.cache/matplotlib rm -rf *.*

    3. 添加字体
    首先在终端中进入你的环境,查看 matplotlib 配置文件位置

    # 切换到你的虚拟环境
    workon '虚拟环境名'
    
    # 进入python交互模式:
    ipython3
    
    # 导入matplotlib包
    import matplotlib
    
    # 查看matplotlib配置文件所在位置
    print(matplotlib.matplotlib_fname())

    # 第二种较为方便查看方式
    import matplotlib as mpl
    print(mpl.get_configdir())

    4. 进入配置文件所在对应文件夹

    将下载的字体放到 fonts/ttf 文件夹里面,(*.ttf放到这个文件夹里面)
    5. 编辑配置文件 matplotlibrc

    # 利用搜索关键字找到下面这一行代码,然后去掉 # (取消注释),添加下载的字体 SimHei (字体名,我的是SimHei)
    
    # 源内容
    #font.sans-serif:DejaVu Sans, Bitstream Vera Sans, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif
    
    修改为
    
    font.sans-serif: SimHei, DejaVu Sans, Bitstream Vera Sans, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif

    重启 jupyter-notebook

    运行成功

     收工

    三、补充安装问题:

      直接pip下载会比较慢,下载的时候指定源会快许多。如下:

    pip install matplotlib -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
    pip install numpy -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
    pip install pandas -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
    pip install seaborn scipy  -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
  • 相关阅读:
    [题解]Magic Line-计算几何(2019牛客多校第三场H题)
    [题解]Crazy Binary String-前缀和(2019牛客多校第三场B题)
    [数论]快速幂取模
    [模板]大整数乘法——累加型
    [动态规划] 最大子段和问题
    2073
    17-2-24-D
    17-1-31-C
    2032
    1992
  • 原文地址:https://www.cnblogs.com/yblackd/p/14533427.html
Copyright © 2011-2022 走看看