python---文字雲

本文介紹的是數據可視化中的一種常見方式:文字雲。字體

用Python構建文字雲主要分爲兩步:spa

1)構建文字雲code

1 from wordcloud import WordCloud
2 
3 wc = WordCloud(background_color="white", max_words=display,\    ### white:爲文字雲的背景顏色; display:爲文字雲最多顯示的詞數目
4                stopwords=more_stopwords,font_path="BROADW.TTF").generate(words1)  ### more_stopwords: 爲要過濾掉的,不在文字雲中顯示的詞; 
5 font_path: 字體; word1 :爲要構建文字雲的目標詞

上面這段代碼就構建了一個文字雲對象wc,接下來的任務就是要把這個對象可視化出來。對象

2)展現文字雲blog

 1 cloudtitle=['MDMA','Amphethamines','Unknown']         ###水平方向的title
 2 cloudy=['Top 50-100','Top 100-150','Top 200-250']     ###豎直方向的title
 3 
 4 fig, axs = plt.subplots(3,3, figsize=(18, 9), facecolor='w', edgecolor='k') 
 5 #axs.set_xlabel('common xlabel')
 6 fig.text(0.5, 0.1, 'Columns Split by SC Cateogry label', ha='center', va='center',fontsize=24)
 7 fig.subplots_adjust(hspace = .05, wspace=.00001)
 8 
 9 axs = axs.ravel()  ### 把圖框進行平鋪,這裏說明畫框內有多個圖,用下標訪問每個圖
10 
11 axs[0].imshow(wc)
12     
13 axs[0].set_title(cloudtitle[0],fontsize=24,fontweight=6)
14 axs[0].set_ylabel(cloudy[0],fontsize=18,fontweight=3)
15     
16 axs[0].set_yticks([]) #-set tick marks to empty list.
17 axs[0].set_xticks([])
18     
19     
20   

效果以下圖所示:it

相關文章
相關標籤/搜索