本文複制源自: https://www.linuxidc.com/Linux/2019-03/157632.htm
當我們用matplotlib作圖時中文亂碼,往往會發現中文的文字變成了小方塊,我在繪制決策樹的時候就碰到了這個問題。
以下是必須能夠解決問題的文法:
1、環境檢視
a.系統版本檢視
[email protected]:~/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.系統中文字型檢視
[email protected]:~/www.linuxidc.com$ fc-list :lang=zh
/usr/share/fonts/truetype/arphic/uming.ttc: AR PL UMing TW MBE:style=Light
/usr/share/fonts/opentype/noto/NotoSansCJK-Bold.ttc: Noto Sans CJK JP,Noto Sans CJK JP Bold:style=Bold,Regular
/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/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-Black.ttc: Noto Sans CJK TC,Noto Sans CJK TC Black:style=Black,Regular
/usr/share/fonts/opentype/noto/NotoSansCJK-Black.ttc: Noto Sans CJK KR,Noto Sans CJK KR Black:style=Black,Regular
/usr/share/fonts/truetype/wqy/wqy-microhei.ttc: WenQuanYi Micro Hei,文泉驛微米黑,文泉驿微米黑:style=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-Medium.ttc: Noto Sans CJK JP,Noto Sans CJK JP Medium:style=Medium,Regular
c. python版本及matplotlib配置檔案位置查詢
Python 3.5.2 (default, Jul 10 2019, 11:58:48)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib
>>> matplotlib.matplotlib_fname()
'/usr/local/lib/python3.5/dist-packages/matplotlib/mpl-data/matplotlibrc'
2. 第一種解決方法: 在代碼中指定字型配置 請注意, 這種方法隻能用于plt.title, plt.xlabel, plt.ylabel, plt.bar裡因為不帶fontproperties變量, 是以不适用
#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.第二種解決辦法 自測這種方法會出錯: font family ['serif'] not found, 但是無論如何這個Error都需要解決, 下邊的方法會解決此問題
首先将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.第三種解決辦法
a) 首先将Windwos中fonts目錄下的SimHei.ttf拷貝到 /usr/local/lib/python3.5/dist-packages/matplotlib/mpl-data/fonts/ttf 目錄中,如果找不到, 從這裡下載下傳: https://github.com/StellarCN/scp_zh/raw/master/fonts/SimHei.ttf
b) 第三修改配置檔案: 這一步可以解決第二種方法裡提到的問題
~$ vi /usr/local/lib/python3.5/dist-packages/matplotlib/mpl-data/matplotlibrc
檔案路徑參考1.c,根據實際情況修改,找到如下兩項,去掉前面的#,并在font.sans-serif冒号後面加上SimHei
font.family : sans-serif
font.sans-serif : SimHei, Bitstream Vera Sans, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif
就是知道字庫族為sans-serif,同時添加“SimHei”即宋體到字庫族清單中
c) 删除~/.cache/matplotlib的緩沖目錄 rm -rf ~/.cache/matplotlib/*