zoukankan      html  css  js  c++  java
  • matplotlib绘图不显示中文

    妈的 anaconda 版本太低,导致matlotlib 画图不清晰, 换个就好了,不然图形总是一坨
    wget https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/Anaconda3-2019.07-Linux-x86_64.sh

    
    import pandas as pd
    import numpy as np
    from pandas import Series, DataFrame
    %matplotlib inline
    %config InlineBackend.figure_format = 'svg'
    

    操作

    下载字体

    http://wt4.jb51.net:81/201509/tools/simhei(jb51.net).rar
    http://down.font5.com.cn/fontfile/200903/simheittf.rar
    http://www.font5.com.cn/font_download.php?id=151&part=1237887120
    http://www.font5.com.cn/font_download.php?id=151&part=1237887120

    获取matplotlib的配置文件位置

    import matplotlib
    print(matplotlib.matplotlib_fname())
    
    #获取到文件:
    /home/data/anaconda3/lib/python3.6/site-packages/matplotlib/mpl-data/matplotlibrc
    ##修改
    vim /home/data/anaconda3/lib/python3.6/site-packages/matplotlib/mpl-data/matplotlibrc
    font.family         : sans-serif
    
    font.sans-serif     : SimHei, DejaVu Sans, Bitstream Vera Sans, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif(在font.sans-serif后添加SimHei,用来正常显示中文标签)
    
    axes.unicode_minus  : False(把True改为False,用来正常显示负号)
    
    

    获取matplotlib 字体库位置

    locate -b 'mpl-data'
    
    /home/data/anaconda3/lib/python3.6/site-packages/matplotlib/mpl-data
    /home/data/anaconda3/pkgs/matplotlib-2.2.2-py36h0e671d2_1/lib/python3.6/site-packages/matplotlib/mpl-data
    
    

    将字体文件复制到这两个路径下。
    终端下运行命令fc-list :lang=zh-cn (不知道是干啥的)

    找到家目录 .cache/matplotlib目录将matplotlib删除

    方法1:改了配置之后并不会生效,需要重新加载字体,在notebook中运行如下代码即可
    from matplotlib.font_manager import _rebuild
    rebuild() #重新加载一下
    2)方法2:如果上面方法1试了以后,还不行。重启电脑。
    

    重启机器

    以下没有测试过.

    下载中文字体:
    http://www.font5.com.cn/font_download.php?id=151&part=1237887120
    
    将下载的字体ttf文件放到matplotlib的字体库位置:
    anaconda3/lib/python3.6/site-packages/matplotlib/mpl-data/fonts/ttf/
    
    删除matplotlib的字体缓存,文件位置:
    mpl.get_cachedir()–>获取字体cache位置的方法(应该有获取其他配置的方法)
    .cache/matplotlib/fontList.json
    该文件删除后,会自动生成,重新查找解析字体库,并生成这个文件
    
    代码中的修改:
    rcParams[‘font.family’] = ‘sans-serif’ #用来正常显示中文
    rcParams[‘font.sans-serif’] = [‘SimHei’] #用来正常显示负号
    
    永久修改:
    mpl.matplotlib_fname() –>获取配置文件的方法
    可在matplotlib的配置文件中修改:/home/rickey/anaconda3/lib/python3.6/site-packages/matplotlib/mpl-data/matplotlibrc
    其中有font.family和font.sans-serif的配置项
    
    font.family和font.sans-serif的联系:
    font.family是一个字体集,其中包含多种字体,比如sans-serif表示中文,中文可以有多种字体,比如正楷,宋体等
    SimHei :黑体
    SimSun :宋体
    以上具体的某一种字体可以在字体网站上下载,并安装。
    
    

    参考:https://www.jianshu.com/p/240ea3ae0dc9

  • 相关阅读:
    耗油
    [深入Python]Alex Martelli的Borg类
    Python Frame objects 和Traceback objects
    2012美国汽车销量排行
    Python中统计函数的运行耗时
    Python显示函数的调用者
    Python的内置函数map
    [深入Python]简单事情复杂化:Python计算阶乘
    Solaris查看线程
    VVR常用操作
  • 原文地址:https://www.cnblogs.com/g2thend/p/12362378.html
Copyright © 2011-2022 走看看