Python簡單實現詞云圖代碼及步驟解析
一、安裝 wordcloud
pip install wordcloud
二、加載包、設置路徑
import osfrom wordcloud import WordCloudimport matplotlib.pyplot as pltos.chdir(’E:pyspacetmp’)
三、詞云圖示例
1、默認參數示例
text = ’Keep it simple and stupid.’wc = WordCloud() # 實例化詞云圖對象wc.generate(text) # 根據文本生成詞云圖plt.imshow(wc) # 顯示詞云圖
如果 jupyter 沒有圖形輸出,需要設置 jupyter 的圖形顯示方式
%matplotlib inline
WordCloud() 詞云圖對象對應的畫布默認長200像素,寬400像素,背景色為黑色。
2、配置參數示例
text = ’Keep it simple and stupid.’wc = WordCloud(background_color=’white’, width=500, height=300) # 實例化詞云圖對象wc.generate(text) # 根據文本生成詞云圖plt.imshow(wc) # 顯示詞云圖
3、不顯示坐標軸
text = ’Keep it simple and stupid.’wc = WordCloud(background_color=’white’, width=500, height=300) # 實例化詞云圖對象wc.generate(text) # 根據文本生成詞云圖plt.imshow(wc) # 顯示詞云圖plt.axis(’off’) # 不顯示坐標軸plt.show()
環境說明:
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。
相關文章: