• linux中使用matplotlib缺失中文字体的解决方案


    linux中使用matplotlib缺失中文字体的解决方案

    在linux中调用matplotlib画图时,如果label中有中文,需要通过以下语句指定字体:

    import matplotlib.pyplot as plt
    plt.rcParams['font.sans-serif']=['SimHei'] 
    

    这时可能会因为系统中缺失中文字体而导致不能显示。具体表现就是,运行过程中报warning:

    /root/anaconda3/lib/python3.7/site-packages/matplotlib/font_manager.py:1241: UserWarning: findfont: Font family ['sans-serif'] not found. Falling back to DejaVu Sans.
      (prop.get_family(), self.defaultFamily[fontext]))
    

    最后得到的图中,中文都变成了黑色方框。

    解决方法:

    首先,在windows或者macOS的字体库中找到可以用的.ttf扩展名的字体库文件。如果没有,可以到网站下载:如:http://www.downcc.com/font/248408.html

    下载的msyh.ttf即Microsoft YaHei,简称msyh。将这个字体库copy到linux存放字体的地方。(首先我们在fonts文件夹下先新建一个存放从macos拷贝过来的字体库文件的目录):

    mkdir /usr/share/fonts/macos
    

    然后在copy(或者mv)

    cp msyh.ttf /usr/share/fonts/macos
    

    下面进入到这个文件夹,执行以下命令:

    cd /usr/share/fonts/macos
    mkfontscale
    mkfontdir
    fc-cache
    

    此时再用下面的命令查看已经安装好的字体:

    fc-list
    

    显示结果:

    /usr/share/fonts/default/ghostscript/hrpld.pfa: Hershey-Plain-Duplex:style=Regular
    /usr/share/fonts/default/Type1/c059016l.pfb: Century Schoolbook L:style=Bold
    /usr/share/fonts/default/Type1/c059033l.pfb: Century Schoolbook L:style=Italic
    /usr/share/fonts/default/Type1/p052024l.pfb: URW Palladio L:style=Bold Italic
    /usr/share/fonts/default/Type1/d050000l.pfb: Dingbats:style=Regular
    /usr/share/fonts/default/ghostscript/hrplt.pfa: Hershey-Plain-Triplex:style=Regular
    /usr/share/fonts/default/ghostscript/hrplti.pfa: Hershey-Plain-Triplex-Italic:style=Regular
    /usr/share/fonts/default/Type1/s050000l.pfb: Standard Symbols L:style=Regular
    /usr/share/fonts/default/ghostscript/bchri.pfa: Bitstream Charter:style=Italic
    /usr/share/fonts/default/Type1/n021003l.pfb: Nimbus Roman No9 L:style=Regular
    /usr/share/fonts/default/Type1/a010013l.pfb: URW Gothic L:style=Book
    /usr/share/fonts/default/Type1/n019003l.pfb: Nimbus Sans L:style=Regular
    /usr/share/fonts/dejavu/DejaVuSansCondensed-Oblique.ttf: DejaVu Sans,DejaVu Sans Condensed:style=Condensed Oblique,Oblique
    /usr/share/fonts/dejavu/DejaVuSansCondensed-Bold.ttf: DejaVu Sans,DejaVu Sans Condensed:style=Condensed Bold,Bold
    /usr/share/fonts/default/Type1/a010033l.pfb: URW Gothic L:style=Book Oblique
    /usr/share/fonts/default/Type1/a010015l.pfb: URW Gothic L:style=Demi
    /usr/share/fonts/default/Type1/n022003l.pfb: Nimbus Mono L:style=Regular
    /usr/share/fonts/default/ghostscript/hrscs.pfa: Hershey-Script-Simplex:style=Regular
    /usr/share/fonts/default/Type1/n022024l.pfb: Nimbus Mono L:style=Bold Oblique
    /usr/share/fonts/default/ghostscript/putbi.pfa: Utopia:style=Bold Italic
    /usr/share/fonts/default/Type1/b018012l.pfb: URW Bookman L:style=Light
    /usr/share/fonts/default/ghostscript/putri.pfa: Utopia:style=Italic
    /usr/share/fonts/dejavu/DejaVuSans.ttf: DejaVu Sans:style=Book
    /usr/share/fonts/default/Type1/n021024l.pfb: Nimbus Roman No9 L:style=Medium Italic
    /usr/share/fonts/default/Type1/a010035l.pfb: URW Gothic L:style=Demi Oblique
    /usr/share/fonts/default/ghostscript/bchr.pfa: Bitstream Charter:style=Regular
    /usr/share/fonts/default/Type1/p052023l.pfb: URW Palladio L:style=Italic
    /usr/share/fonts/default/ghostscript/bchbi.pfa: Bitstream Charter:style=Bold Italic
    /usr/share/fonts/default/Type1/c059013l.pfb: Century Schoolbook L:style=Roman
    /usr/share/fonts/default/Type1/n021004l.pfb: Nimbus Roman No9 L:style=Medium
    /usr/share/fonts/default/ghostscript/hrger.pfa: Hershey-Gothic-English:style=Regular
    /usr/share/fonts/default/Type1/b018035l.pfb: URW Bookman L:style=Demi Bold Italic
    /usr/share/fonts/default/Type1/n022023l.pfb: Nimbus Mono L:style=Regular Oblique
    /usr/share/fonts/default/Type1/p052004l.pfb: URW Palladio L:style=Bold
    /usr/share/fonts/default/Type1/n022004l.pfb: Nimbus Mono L:style=Bold
    /usr/share/fonts/default/ghostscript/hritr.pfa: Hershey-Gothic-Italian:style=Regular
    /usr/share/fonts/default/ghostscript/hrpldi.pfa: Hershey-Plain-Duplex-Italic:style=Regular
    /usr/share/fonts/default/Type1/b018032l.pfb: URW Bookman L:style=Light Italic
    /usr/share/fonts/default/Type1/n019023l.pfb: Nimbus Sans L:style=Regular Italic
    /usr/share/fonts/dejavu/DejaVuSans-Bold.ttf: DejaVu Sans:style=Bold
    /usr/share/fonts/default/ghostscript/hrgrr.pfa: Hershey-Gothic-German:style=Regular
    /usr/share/fonts/default/Type1/b018015l.pfb: URW Bookman L:style=Demi Bold
    /usr/share/fonts/default/ghostscript/putb.pfa: Utopia:style=Bold
    /usr/share/fonts/default/Type1/n021023l.pfb: Nimbus Roman No9 L:style=Regular Italic
    /usr/share/fonts/default/Type1/n019024l.pfb: Nimbus Sans L:style=Bold Italic
    /usr/share/fonts/default/Type1/z003034l.pfb: URW Chancery L:style=Medium Italic
    /usr/share/fonts/default/Type1/n019004l.pfb: Nimbus Sans L:style=Bold
    /usr/share/fonts/default/ghostscript/putr.pfa: Utopia:style=Regular
    /usr/share/fonts/dejavu/DejaVuSansCondensed.ttf: DejaVu Sans,DejaVu Sans Condensed:style=Condensed,Book
    /usr/share/fonts/dejavu/DejaVuSans-ExtraLight.ttf: DejaVu Sans,DejaVu Sans Light:style=ExtraLight
    /usr/share/fonts/macos/msyh.ttf: Microsoft YaHei:style=Regular,Normal
    /usr/share/fonts/dejavu/DejaVuSansCondensed-BoldOblique.ttf: DejaVu Sans,DejaVu Sans Condensed:style=Condensed Bold Oblique,Bold Oblique
    /usr/share/fonts/dejavu/DejaVuSans-Oblique.ttf: DejaVu Sans:style=Oblique
    /usr/share/fonts/default/Type1/n019044l.pfb: Nimbus Sans L:style=Bold Condensed
    /usr/share/fonts/default/Type1/p052003l.pfb: URW Palladio L:style=Roman
    /usr/share/fonts/default/Type1/c059036l.pfb: Century Schoolbook L:style=Bold Italic
    /usr/share/fonts/default/Type1/n019063l.pfb: Nimbus Sans L:style=Regular Condensed Italic
    /usr/share/fonts/default/Type1/n019064l.pfb: Nimbus Sans L:style=Bold Condensed Italic
    /usr/share/fonts/default/ghostscript/hrscc.pfa: Hershey-Script-Complex:style=Regular
    /usr/share/fonts/dejavu/DejaVuSans-BoldOblique.ttf: DejaVu Sans:style=Bold Oblique
    /usr/share/fonts/default/Type1/n019043l.pfb: Nimbus Sans L:style=Regular Condensed
    /usr/share/fonts/default/ghostscript/bchb.pfa: Bitstream Charter:style=Bold
    

    可以看到里面有这样一条:

    /usr/share/fonts/macos/msyh.ttf: Microsoft YaHei:style=Regular,Normal
    

    这就是我们刚刚装上的雅黑,冒号后面就是字体名称的全称。下面再设置matplotlib:

    import matplotlib.pyplot as plt
    plt.rcParams['font.sans-serif']=['Microsoft YaHei'] 
    
    

    此时就可以应用该字体作为图片上中文标签的字体了~~~

    2019-08-29 14:48:40

  • 相关阅读:
    openstack私有云布署实践【7.2 keystone + memcache (办公网环境)】
    openstack私有云布署实践【2 安装前的服务器基本环境准备】
    openstack私有云布署实践【11.1 计算nova compute节点配置(科兴环境)】
    openstack私有云布署实践【4.2 上层代理haproxy+nginx配置 (办公网测试环境)】
    openstack私有云布署实践【9.1 Glance镜像管理(科兴环境)】
    openstack私有云布署实践【7.1 keystone + memcache (科兴环境)】
    openstack私有云布署实践【10.1 计算nova kxcontroller节点配置(科兴环境)】
    openstack私有云布署实践【6 RabbitMQ】
    openstack私有云布署实践【4.1 上层代理haproxy配置 (科兴环境)】
    openstack私有云布署实践【8.2 身份认证keystone的API创建(办公网环境)】
  • 原文地址:https://www.cnblogs.com/morikokyuro/p/13256670.html
Copyright © 2020-2023  润新知