in this paper, we implement a text cloud diagram of arbitrary graphs using Python
modules to use
The text cloud requires two modules: Jieba and Wordcloud, which can be installed by using the Pip method:
Pip Install Jieba/wordcloud
When you install Wordcloud, you may experience an error:
This situation can be installed in a different way, under the http://www.lfd.uci.edu/~gohlke/pythonlibs/#wordcloud find wordcloud download link, download the corresponding WHL file, After downloading the path to store the file, perform "Pip Install WORDCLOUD‑1.3.2‑CP35‑CP35M‑WIN_AMD64.WHL" on the OK.
train of Thought
First use Jieba participle of the article to the word, while reading the background of the picture (used to draw different shapes of text Touyun), and then through the Wordcloud generation of text-map cloud and drawing tools to show.
Sample
The "gentleman" lyrics to make a mean-shaped word cloud to show
Implementation Code
#分词模块
Import Jieba
#画图模块
import matplotlib.pyplot as Plt
#文字云模块 from
wordcloud import Wordcloud
#读取背景图片 from
scipy.misc import imread
#文件名
word_file= ' word.txt '
#读取文件内容
word_content= Open (Word_file, ' R ', encoding= ' utf-8 '). Read (). replace (' \ n ', ')
#设置背景图片
img_file= ' heart.jpg '
Read background picture
Mask_img=imread (img_file)
#进行分词
word_cut=jieba.cut (word_content)
#把分词用空格连起来
Word_cut_join= "". Join (Word_cut)
#生成词云
wc=wordcloud (
font_path= ' Msyh.ttf ', #设置字体
max_words= #词云显示的最大词数
mask=mask_img, #设置背景图片
background_color= ' white ' #背景颜色
). Generate (Word_cut_join)
plt.imshow (WC)
#去掉坐标轴
plt.axis (' off ')
#将图片保存到本地
plt.savefig (' heart_cy.jpg ')
Plt.show ()
Build Results