How Python generates word clouds

Source: Internet
Author: User
This article mainly introduces the method of how Python generates word cloud, has certain reference value, now share to everybody, need friend can refer to

Objective

Today I teach you to use the Wrodcloud module to generate the word cloud, I read a novel and generated a word cloud, first look at:

One:

Two:

According to the analysis or relatively accurate, the protagonist in the novel is "course", so the number of occurrences most. There are two modes in the diagram, one is the default mode, the other is to add a picture yourself as the background. Here I will take you to learn how to create a word cloud!

Installation of Wordcloud

For new people to install the Wordcloud module is a mark, we generally are installed through the Pycharm or PIP, but the installation of Wordcloud will prompt errors, as follows:

My solution is to visit the following URL directly, according to your computer and the PY version download the corresponding WHL file

After the download is complete, put the WHL file in the PIP path, enter the following code, remember that the WHL filename of the code is appropriate for your file name, I just example:

Pip Install WORDCLOUD-1.4.1-CP36-CP36M-WIN32.WHL

After running, the installation will be displayed successfully.

Simple usage of Wordcloud

Wordcloud The basic method, see the source code as follows:

Each parameter should be understood at a glance, there are several other methods Wordcloud, such as fit_words,generate (text), to_file (filename) and so on are very good understanding.

Wordcloud Generating Word clouds

One of the code:

From Wordcloud import Wordcloudimport matplotlib.pyplot as Plt #绘制图像的模块import jieba     #jieba分词path_txt = ' c://users/ Administrator/desktop/all.txt ' F = open (Path_txt, ' R ', encoding= ' UTF-8 '). Read () # stutter participle, generate string, Wordcloud cannot directly generate the correct Chinese word cloud Cut_text = "". Join (Jieba.cut (f)) Wordcloud = Wordcloud (#设置字体, otherwise there will be garbled characters, the path of the text is the general path of the computer's font, can be replaced by other Font_path= "C:/windows/fonts/simfang.ttf", #设置了背景, Wide high background_color= "white", width=1000,height=880). Generate (Cut _text) plt.imshow (Wordcloud, interpolation= "bilinear") Plt.axis ("Off") Plt.show ()

After running:

Second, add a background to the word cloud, the code is as follows:

From PIL import imagefrom wordcloud import Wordcloud, Imagecolorgeneratorimport Matplotlib.pyplot as Pltimport NumPy as NP Import Jiebadef Getwordcloud (): path_txt = ' c://users/administrator/desktop/all.txt ' path_img = ' C://Users/ Administrator/desktop/timg.jpg "f = open (Path_txt, ' R ', encoding= ' UTF-8 '). Read () Background_image = Np.array ( Image.open (path_img) # stutter participle, generate strings, if not through the word, can not directly generate the correct Chinese word cloud, interested friends to check, there are a variety of word patterns #Python join () method is used to generate a new string from the elements in the sequence with the specified character connection. Cut_text = "". Join (Jieba.cut (f)) Wordcloud = Wordcloud (  # Set the font, otherwise there will be garbled characters, the text path is the general path of the computer's font, can be replaced by other  font_path= "C :/windows/fonts/simfang.ttf ",  background_color=" White ",  # Mask parameter = Picture background, must be written on, in addition to the mask parameter and then set the width of high is invalid  mask =background_image). Generate (Cut_text) # generate color Values image_colors = Imagecolorgenerator (background_image) # The following code indicates the display picture Plt.imshow (Wordcloud.recolor (color_func=image_colors), interpolation= "bilinear") Plt.axis ("Off") plt.show () if __ name__ = = ' __main__ ': Getwordcloud ()

After running:

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.