Generate word Cloud with Python

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.