首頁 > 軟體

完美解決Python下matplotlib繪圖中文亂碼問題

2020-06-16 16:38:00

當我們用matplotlib作圖時中文亂碼,往往會發現中文的文字變成了小方塊,我在繪制決策樹的時候就碰到了這個問題。將各種方式試了個遍,都不管用……最終發現是沒有刪除matplotlib下字型的快取目錄,即~/.cache/matplotlib

下面記錄找到的比較好的方法,以便下次使用。

1、環境檢視

a.系統版本檢視

linuxidc@linuxidc:~/www.linuxidc.com$ cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=18.04
DISTRIB_CODENAME=bionic
DISTRIB_DESCRIPTION="Ubuntu 18.04.2 LTS"

b.系統中文字型檢視

linuxidc@linuxidc:~/www.linuxidc.com$ fc-list :lang=zh
/usr/share/fonts/truetype/arphic/uming.ttc: AR PL UMing TW MBE:style=Light
/home/linuxidc/.local/share/fonts/方正準圓簡體.TTF: 方正準圓簡體,FZZhunYuan-M02S:style=Regular
/usr/share/fonts/opentype/noto/NotoSansCJK-Bold.ttc: Noto Sans CJK JP,Noto Sans CJK JP Bold:style=Bold,Regular
/usr/share/fonts/opentype/noto/NotoSerifCJK-Bold.ttc: Noto Serif CJK SC:style=Bold
/usr/share/fonts/opentype/noto/NotoSerifCJK-Bold.ttc: Noto Serif CJK TC:style=Bold
/usr/share/fonts/truetype/arphic/ukai.ttc: AR PL UKai CN:style=Book
/usr/share/fonts/truetype/arphic/ukai.ttc: AR PL UKai HK:style=Book
/usr/share/fonts/opentype/noto/NotoSerifCJK-Bold.ttc: Noto Serif CJK JP:style=Bold
/usr/share/fonts/opentype/noto/NotoSerifCJK-Bold.ttc: Noto Serif CJK KR:style=Bold
/usr/share/fonts/truetype/arphic/ukai.ttc: AR PL UKai TW:style=Book
/usr/share/fonts/opentype/noto/NotoSansCJK-Bold.ttc: Noto Sans Mono CJK KR,Noto Sans Mono CJK KR Bold:style=Bold,Regular
/usr/share/fonts/opentype/noto/NotoSansCJK-Bold.ttc: Noto Sans Mono CJK JP,Noto Sans Mono CJK JP Bold:style=Bold,Regular
/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc: Noto Sans CJK JP,Noto Sans CJK JP Regular:style=Regular
/usr/share/fonts/opentype/noto/NotoSansCJK-Bold.ttc: Noto Sans Mono CJK TC,Noto Sans Mono CJK TC Bold:style=Bold,Regular
/usr/share/fonts/opentype/noto/NotoSerifCJK-Regular.ttc: Noto Serif CJK SC:style=Regular
/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc: Noto Sans CJK KR,Noto Sans CJK KR Regular:style=Regular
/usr/share/fonts/opentype/noto/NotoSerifCJK-Regular.ttc: Noto Serif CJK TC:style=Regular
/usr/share/fonts/opentype/noto/NotoSerifCJK-Regular.ttc: Noto Serif CJK JP:style=Regular
/usr/share/fonts/opentype/noto/NotoSerifCJK-Regular.ttc: Noto Serif CJK KR:style=Regular
/usr/share/fonts/opentype/noto/NotoSansCJK-Bold.ttc: Noto Sans Mono CJK SC,Noto Sans Mono CJK SC Bold:style=Bold,Regular
/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc: Noto Sans Mono CJK JP,Noto Sans Mono CJK JP Regular:style=Regular
/usr/share/fonts/truetype/droid/DroidSansFallbackFull.ttf: Droid Sans Fallback:style=Regular
/usr/share/fonts/opentype/noto/NotoSansCJK-Bold.ttc: Noto Sans CJK TC,Noto Sans CJK TC Bold:style=Bold,Regular
/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc: Noto Sans Mono CJK KR,Noto Sans Mono CJK KR Regular:style=Regular
/usr/share/fonts/opentype/noto/NotoSansCJK-Bold.ttc: Noto Sans CJK SC,Noto Sans CJK SC Bold:style=Bold,Regular
/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc: Noto Sans CJK SC,Noto Sans CJK SC Regular:style=Regular
/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc: Noto Sans CJK TC,Noto Sans CJK TC Regular:style=Regular
/usr/share/fonts/truetype/arphic/ukai.ttc: AR PL UKai TW MBE:style=Book
/home/linuxidc/.local/share/fonts/SIMHEI.TTF: 黑体,SimHei:style=Regular,Normal,oby?ejné,Standard,Κανονικ?,Normaali,Normál,Normale,Standaard,Normalny,Обычный,Normálne,Navadno,Arrunta
/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc: Noto Sans Mono CJK TC,Noto Sans Mono CJK TC Regular:style=Regular
/usr/share/fonts/truetype/arphic/uming.ttc: AR PL UMing TW:style=Light
/usr/share/fonts/truetype/arphic/uming.ttc: AR PL UMing CN:style=Light
/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc: Noto Sans Mono CJK SC,Noto Sans Mono CJK SC Regular:style=Regular
/usr/share/fonts/truetype/arphic/uming.ttc: AR PL UMing HK:style=Light
/home/linuxidc/.local/share/fonts/文泉驛正黑.ttf: 文泉驛正黑,文泉驛正黑,WenQuanYi Zen Hei:style=Medium,中等
/home/linuxidc/.local/share/fonts/方正水柱_GBK.ttf: 方正水柱_GBK,FZShuiZhu-M08:style=Regular
/usr/share/fonts/opentype/noto/NotoSansCJK-Bold.ttc: Noto Sans CJK KR,Noto Sans CJK KR Bold:style=Bold,Regular

c.Python版本及matplotlib組態檔位置查詢

linuxidc@linuxidc:~/www.linuxidc.com$ python
Python 2.7.15rc1 (default, Nov 12 2018, 14:31:15)
[GCC 7.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib
>>> matplotlib.matplotlib_fname()
'/usr/local/lib/python2.7/dist-packages/matplotlib/mpl-data/matplotlibrc'
>>>

2、第一種解決方法

在程式碼中指定字型設定

#coding:utf-8 
import matplotlib 
matplotlib.use('qt4agg') 
from matplotlib.font_manager import * 
import matplotlib.pyplot as plt 
#定義自定義字型,檔名從1.b檢視系統中文字型中來 
myfont = FontProperties(fname='/home/linuxidc/.local/share/fonts/文泉驛正黑.ttf') 
#解決負號'-'顯示為方塊的問題 
matplotlib.rcParams['axes.unicode_minus']=False 
plt.plot([-1,2,-5,3]) 
plt.title(u'Linux公社的網址是www.linuxidc.com',fontproperties=myfont) 
plt.show()

3.第二種解決辦法

首先將Windwos中fonts目錄下的simhei.ttf拷貝到  /usr/local/lib/python2.7/dist-packages/matplotlib/mpl-data/fonts/ttf  (檔案路徑參考1.c,根據實際情況修改)目錄中,

然後刪除~/.cache/matplotlib的緩衝目錄

第三在程式碼中動態設定引數:

#coding:utf-8 
import matplotlib 
matplotlib.use('qt4agg') 
#指定預設字型 
matplotlib.rcParams['font.sans-serif'] = ['SimHei'] 
matplotlib.rcParams['font.family']='sans-serif' 
#解決負號'-'顯示為方塊的問題 
matplotlib.rcParams['axes.unicode_minus'] = False 
plt.plot([-1,2,-5,3]) 
plt.title(u'Linux公社的網址是www.linuxidc.com',fontproperties=myfont) 
plt.show()

4.第三種解決辦法
首先將Windwos中fonts目錄下的simhei.ttf拷貝到 /usr/local/lib/python2.7/dist-packages/matplotlib/mpl-data/fonts/ttf  目錄中,
然後刪除~/.cache/matplotlib的緩衝目錄

第三修改修改組態檔:
linuxidc@linuxidc:~$ vi /usr/local/lib/python2.7/dist-packages/matplotlib/mpl-data//matplotlibrc


IT145.com E-mail:sddin#qq.com