matplotlib默認字體設置

使用matplotlib的pyplot時,是支持unicode的,但默認字體是英文字體,致使中文沒法正常顯示。
解決方法有兩種。windows

一種是手動指定,在代碼中字體

from matplotlib.font_manager import FontProperties  
font_song = FontProperties(fname=r"c:\windows\fonts\simsun.ttc", size=15)

使用文字時指定參數 fontproperties=font_song 便可。code

另外一種是修改配置文件中的默認字體,須要修改 matplotlibrc 文件。
這一文件的位置能夠使用 matplotlib.matplotlib_fname() 查看。
若是沒有修改會在site-package中。
在默認的matplotlibrc中有註釋,建議保存在$HOME/.matplotlib/matplotlibrc下(win環境),防止在更新包時配置文件被覆蓋。
經過 font.family : sans-serif 能夠指定默認字體族爲 sans-serif
以後在 font.sans-serif 的配置列表中最前加入中文字體便可。
但使用的中文字體應該爲中文字體的名稱。
查看系統的名稱能夠經過font_manager查看。
[ f.name for f in matplotlib.font_manager.fontManager.ttflist ]
[ f.name for f in matplotlib.font_manager.fontManager.afmlist ]
這兩個列表。
我使用的win10沒有SimSun(宋體),只能使用STSong,不知是何緣由。
另外微軟官網字體裏也找不到win10信息(官網連接)。unicode

相關文章
相關標籤/搜索