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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章