zoukankan      html  css  js  c++  java
  • matplotlib、seaborn 展示中文字体

    matplotlib、seaborn 展示中文字体

    我们经常使用matplotlibseaborn绘制各种图标但是失望的是他们对中文字符不支持。

    matplotlib 展示中文

    查找系统上中文字体的安装位置,我选用的是宋体,然后拷贝到可读目录下~/fonts/Songti.ttc

    mac: /System/Library/Fonts/Supplemental/Songti.ttc
    window: C:WINDOWSFonts中文字体.ttf
    linux: /usr/share/fonts
    

    python 代码:

    import matplotlib.font_manager as fm,os
    import matplotlib.pyplot as plt
    
    # 添加字体
    myfont = fm.FontProperties(fname=os.path.join(os.getenv('HOME'),'fonts/Songti.ttc'),size=10)
    plt.xlabel('长度',fontproperties=myfont)
    plt.ylabel('宽度',fontproperties=myfont)
    

    seaborn 展示中文

    方法一:提供字体文件

    1. 查找系统上中文字体的安装位置,我选用的是宋体,然后拷贝到可读目录下~/fonts/Songti.ttc

      mac: /System/Library/Fonts/Supplemental/Songti.ttc
      window: C:WINDOWSFonts中文字体.ttf
      linux: /usr/share/fonts
      
    2. Python 代码:

      import os
      import matplotlib.font_manager as fm
      import seaborn as sns
      import pandas as pd
      
      # 添加字体
      myfont = fm.FontProperties(fname=os.path.join(os.getenv('HOME'),'fonts/Songti.ttc'),size=10)
      sns.set(font=myfont.get_name())
      
      tips = pd.DataFrame({
          'time':["午餐","晚餐"],
          'total_bill':[1,2],
      })
      sns.pointplot(x="time", y="total_bill", data=tips)
      

    方法二:添加字体

    1. 查看 matplotlib 默认字体

       from matplotlib.font_manager import findfont, FontProperties
      
       findfont(FontProperties(family=FontProperties().get_family()))
      # out: /home/xxx/venv/lib/python3.6/site-packages/matplotlib/mpl-data/fonts/ttf/DejaVuSans.ttf
      
    2. 查找 matplotlibrc的路径

      import matplotlib
      
      matplotlib.matplotlib_fname()
      # out: /home/xxx/venv/lib64/python3.6/site-packages/matplotlib/mpl-data/matplotlibrc 
      
    3. 修改matplotlibrc

      vim /home/xxx/venv/lib64/python3.6/site-packages/matplotlib/mpl-data/matplotlibrc 
      

      取消注释font.familyfont.sans-serif ,然后在font.sans-serif后追加你刚才的字符集名:

      # 获取字符集名
      import matplotlib.font_manager as fm
      
      myfont = fm.FontProperties(fname=os.path.join(os.getenv('HOME'),'fonts/Songti.ttc'),size=10)
      myfont.get_name()
      
      # out: Songti SC
      

      修改:

      font.family: sans-serif 
      #font.style:   normal
      #font.variant: normal
      #font.weight:  normal
      #font.stretch: normal
      #font.size:    10.0
      
      #font.serif:      DejaVu Serif, Bitstream Vera Serif, Computer Modern Roman, New Century Schoolbook, Century Schoolbook L,Utopia, ITC Bookman, Bookman, Nimbus Roman No9 L, Times New Roman, Times, Palatino, Charter, serif
      font.sans-serif: Songti SC, DejaVu Sans, Bitstream Vera Sans, Computer Modern Sans Serif, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif
      #font.cursive:    Apple Chancery, Textile, Zapf Chancery, Sand, Script MT, Felipa, cursive
      #font.fantasy:    Comic Neue, Comic Sans MS, Chicago, Charcoal, ImpactWestern, Humor Sans, xkcd, fantasy
      #font.monospace:  DejaVu Sans Mono, Bitstream Vera Sans Mono, Computer Modern Typewriter, Andale Mono, Nimbus Mono L, Courier New, Courier, Fixed, Terminal, monospace
      
    4. 移除matplotlib缓存

      rm -rf ~/.matplotlib
      
    5. 拷贝你的字体文件到matplotlib字体目录下

      cp /System/Library/Fonts/Supplemental/Songti.ttc  /Users/Andrew/miniconda3/envs/technical-note/lib/python3.6/site-packages/matplotlib/mpl-data/fonts/ttf
      
    6. 重启jupyter Notebook 内核,然后就可以了。

    方法三:直接替换默认字体

    1. 查看matplotlib默认字体

       from matplotlib.font_manager import findfont, FontProperties
      
       findfont(FontProperties(family=FontProperties().get_family()))
      # out: /home/xxx/venv/lib/python3.6/site-packages/matplotlib/mpl-data/fonts/ttf/DejaVuSans.ttf
      
    2. 备份字体

      cp /home/xxx/venv/lib/python3.6/site-packages/matplotlib/mpl-data/fonts/ttf/DejaVuSans.ttf /home/xxx/venv/lib/python3.6/site-packages/matplotlib/mpl-data/fonts/ttf/DejaVuSans.ttf.bp
      
    3. 替换

      cp /System/Library/Fonts/Supplemental/Songti.ttc /home/xxx/venv/lib/python3.6/site-packages/matplotlib/mpl-data/fonts/ttf/DejaVuSans.ttf
      
    4. 重启jupyter Notebook 内核,然后就可以了

    如果是此文是转载文章,本人会附上转载链接,此篇文章的版权归原创作者所属,如果侵权请与我联系,我会删除此文。

    若没有标明转载链接,此篇文章属于本人的原创文章,其版权所属:
    作者:feiquan
    出处:http://www.cnblogs.com/feiquan/
    版权声明:本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
    大家写文都不容易,请尊重劳动成果~ 这里谢谢大家啦(*/ω\*)
  • 相关阅读:
    git命令无法自动补全(sles11.3)
    linux下安装svn出现configure: error: We require OpenSSL; try --with-openssl
    Linux系统下升级Python版本步骤(suse系统)
    git commit --amend
    关于device tree中的interrupts选项
    BufferedInputStream
    FileInputStream
    FileOutputStream
    泛型上下限
    泛型接口
  • 原文地址:https://www.cnblogs.com/feiquan/p/14433595.html
Copyright © 2011-2022 走看看