matplotlib標籤出現亂碼解決方案

問題來源

def wx_show_sex_infor(data):
    labers = ['男性','女性','未標記']
    data = [data['male'],data['female'],data['other']]
    plt.pie(data,labels=labers,autopct='%.2f',shadow=True)
    plt.show()
    plt.savefig('sex.jpg')
    plt.close()

問題:生成餅圖標籤出現亂碼

解決方案——修改matplotlibrc

  1. 選取中文字體
  2. 修改matplotlibrc
  3. 測試

1. 選取中文字體

中文字體以simsun爲例,在 C:\Windows\Fonts下面找到“simsun”字體。matplotlib默認安裝在 %PythonPath%\Lib\site-packages目錄下。

我的目錄:D:\Visual Studio\shared\Python37_64\Lib\site-packages\matplotlib\mpl-data\fonts\ttf

複製simsun字體到 %PythonPath%\Lib\site-packages\matplotlib\mpl-data\fonts\ttf\

2. 修改matplotlibrc

  • 找到font.family,改爲font.family:monospace注意消除前面的#
  • 換行添加:font.monospace : simsun

3. 測試

在這裏插入圖片描述

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