天天看點

資料美化 | Python詞雲wordcloud

資料美化 | Python詞雲wordcloud
資料美化 | Python詞雲wordcloud

什麼是詞雲

詞雲是一個可以以優雅,美觀的方式對“詞”進行展示的一個優秀的Python第三方庫。

就如封面圖一樣,可以對你的文本資料按照詞語的出現頻次進行直覺,優美的展示。

資料美化 | Python詞雲wordcloud

安裝詞雲

pip3 install wordcloud      
資料美化 | Python詞雲wordcloud

畫出屬于你的第一個詞雲

待處理的文本:

In Python from the Very Beginning John Whitington takes a no-prerequisites approach to teaching a modern general-purpose programming language. Each small, self-contained chapter introduces a new topic, building until the reader can write quite substantial programs. There are plenty of questions and, crucially, worked answers and hints.

Python from the Very Beginning will appeal both to new programmers, and to experienced programmers eager to explore a new language. It is suitable both for formal use within an undergraduate or graduate curriculum, and for the interested amateur.      

代碼:

from wordcloud import WordCloud
from matplotlib import pyplot as plt

# 擷取文本對象
f = open('C:\\Users\\Administrator\\Desktop\\wd.txt', 'r')

# 讀取文本内容
txt = f.read()

# 建立詞雲對象
wd = WordCloud(background_color = 'pink', width = 1000, height = 500,margin = 0)

# 使用詞雲對文本進行分詞和詞雲的生成
wd_cut = wd.generate(txt)

# plt生成詞雲圖檔
plt.imshow(wd_cut)

# 關閉坐标軸
plt.axis("off")

# 互動式展示詞雲圖
plt.show()

# 儲存詞雲圖檔
wd_cut.to_file('wd.jpg')      

結果:

資料美化 | Python詞雲wordcloud

關閉互動式視窗:

在目前路徑下生成wd.jpg

資料美化 | Python詞雲wordcloud