解決plt中文亂碼 RuntimeWarning_ Glyph 24402 missing from current font. font.set_text(s, 0, flags=flags)


mean1,mean2,mean3=avgSlove(X,y)

label_list = ['色調', '紅色均值', '相對紅色分量', '粗度','高頻能量']    # 橫坐標刻度顯示值
xx = range(len(label_list))
rects1 = plt.bar(x=xx, height=mean1, width=0.4, alpha=0.8,align='edge', color='blue')
plt.ylim(0, 1)     # y軸取值范圍
plt.ylabel("歸一化后的值")
plt.xticks([index + 0.2 for index in xx], label_list)
plt.xlabel("特征")
plt.show()

          如上代碼產生了如下結果

在這里插入圖片描述

          中文的橫坐標沒弄出來,控制台報 RuntimeWarning: Glyph 24402 missing from current font. font.set_text(s, 0, flags=flags)

          查閱資料 https://github.com/matplotlib/matplotlib/issues/15062 發現應該問題是Matplotlib無法正確找到字體並回退到不支持漢字的字體。

          https://www.programmersought.com/article/93394200127/ 找到了解決辦法

解決辦法

          添加 plt.rcParams['font.sans-serif']=['SimHei'] #用來正常顯示中文標簽

mean1,mean2,mean3=avgSlove(X,y)

plt.rcParams['font.sans-serif']=['SimHei'] #用來正常顯示中文標簽
label_list = ['色調', '紅色均值', '相對紅色分量', '粗度','高頻能量']    # 橫坐標刻度顯示值
xx = range(len(label_list))
rects1 = plt.bar(x=xx, height=mean1, width=0.4, alpha=0.8,align='edge', color='blue')
plt.ylim(0, 1)     # y軸取值范圍
plt.ylabel("歸一化后的值")
plt.xticks([index + 0.2 for index in xx], label_list)
plt.xlabel("特征")
plt.show()

在這里插入圖片描述


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM