1. 程式人生 > >Python中matplotlib影象裡的中文輸出問題

Python中matplotlib影象裡的中文輸出問題

Python, matplotlib影象裡的中文輸出問題

1.Python設定中文編碼:

import sys
reload(sys)
sys.setdefaultencoding('utf8')

2.Python使用matplotlib想要在圖中輸出漢字需要設定類似

plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus'] =False #減號unicode編碼

3.Python裡的的字型型別,初始編碼型別為str,用GBK編碼,型別為unicode

text = open('summary.txt'
).read() print type(text) text = text.decode('GBK') print type(text)

4.Python wordcloud想要輸出中文,需要在WordCloud建構函式裡設定font_path,找到其需要的字型路徑例如:’C:\Windows\Fonts\STXIHEI.TTF’

(Tips:使用plt.savefig(‘a.jpg’)儲存圖片注意,一定要在plt.show()之前,plt.show()之後plt就成為一個空白影象了,輸出路徑時要用/,用反斜槓的話會報錯)