問題來源
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
- 選取中文字體
- 修改matplotlibrc
- 測試
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