from wordcloud import WordCloud import matplotlib.pyplot as plt import jieba # 生成詞雲 def create_word_cloud(filename): with open('hongloumong.txt',encod ...
from wordcloud import WordCloud import matplotlib.pyplot as plt import jieba # 生成詞雲 def create_word_cloud(filename): with open('hongloumong.txt',encoding='utf-8') as f: text = f.read() wordlist = jieba.cut(text, cut_all=True) # 結巴分詞 wl = " ".join(wordlist) # 設置詞雲 wc = WordCloud( # 設置背景顏色 background_color="black", # 設置最大顯示的詞雲數 max_words=2000, # 這種字體都在電腦字體中,一般路徑 font_path='msyh.ttc', height=1200, width=1600, # 設置字體最大值 max_font_size=100, # 設置有多少種隨機生成狀態,即有多少種配色方案 random_state=100, ) myword = wc.generate(wl) # 生成詞雲 # 展示詞雲圖 plt.imshow(myword) plt.axis("off") plt.show() wc.to_file('img_book.png') # 把詞雲保存下 if __name__ == '__main__': create_word_cloud('hongloumong')