失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > linux中使用matplotlib缺失中文字体的解决方案

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

时间:2020-05-10 06:24:52

相关推荐

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

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

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

import matplotlib.pyplot as pltplt.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扩展名的字体库文件。如果没有,可以到网站下载:如:/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/macosmkfontscalemkfontdirfc-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/n02l.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/p05l.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/n02l.pfb: Nimbus Mono L:style=Regular Oblique/usr/share/fonts/default/Type1/p05l.pfb: URW Palladio L:style=Bold/usr/share/fonts/default/Type1/n02l.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/p05l.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 pltplt.rcParams['font.sans-serif']=['Microsoft YaHei']

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

-08-29 14:48:40

如果觉得《linux中使用matplotlib缺失中文字体的解决方案》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。