解決matplotlib畫圖中文亂碼

一、下載字體

以SimHei字體爲例,下載SimHei.ttf文件
在python環境下輸入

import matplotlib
print(matplotlib.__path__)

輸出matplotlib的安裝環境,放在該路徑下的mpl-data/fonts/ttf/

二、修改配置文件

修改matplotlib的安裝環境下的,mpl-data/matplotlibrc文件,解開以下三行的註釋

font.family         : sans-serif        
font.sans-serif     : SimHei, Bitstream Vera Sans, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif   
axes.unicode_minus:False

三、代碼reload

在代碼中加入

from matplotlib.font_manager import _rebuild
_rebuild() #reload一下

大功告成

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