current position:Home>Wordcloud Library in Python and its application

Wordcloud Library in Python and its application

2022-02-02 14:44:35 Rocky_ ninety-six

Word cloud takes words as the basic unit , More intuitive and artistic display of text .wordcloud The word cloud is excellent python Third party Library .
The premise is to complete wordcloud The installation of the library :(cmd Command line ) pip install wordcloud.
Then load... In the program wordcloud library .

Usage method :

 step 1: Configure object parameters 
 step 2: Load word cloud text 
 step 3: Output word cloud file 

step 1: Object parameter configuration

w = wordcloud.WordCloud(< Parameters >)
Parameters include :

Parameters meaning Example
width Specifies the width of the image generated by the word cloud object , Default 400 Pixels w=wordcloud.WordCloud(width=600)
height Specifies the height of the image generated by the word cloud object , Default 200 Pixels w=wordcloud.WordCloud(height=400)
in_font_size Specifies the minimum size of the font in the word cloud , Default 4 Number w=wordcloud.WordCloud(min_font_size=10)
max_font_size Specifies the maximum font size of the font in the word cloud , Automatically adjust according to the height w=wordcloud.WordCloud(max_font_size=20)
font_step Specifies the step interval of font size in the word cloud , The default is 1 w=wordcloud.WordCloud(font_step=2)
font_path Specify the path to the font file , Default None w=wordcloud.WordCloud(font_path=“msyh.ttc”)
max_words Specifies the maximum number of words displayed in the word cloud , Default 200 w=wordcloud.WordCloud(max_words=20)
stop_words Specifies the list of excluded words for the word cloud , A list of words that are not displayed w=wordcloud.WordCloud(stop_words={“Python”})
mask Specify the word cloud shape , The default is rectangle , Need to quote imageio() function #import imageio #mk=imageio.imread(“pic.png”) #w=wordcloud.WordCloud(mask=mk)
background_color Specify the background color of the word cloud image , Default is black w=wordcloud.WordCloud(background_color=“white”)

step 2: Load word cloud text

Method meaning
w.generate(txt) towards WordCloud object w Load text in txt

step 3: Output word cloud file

Method meaning
w.to_file(filename) Output word cloud as image file ,.png or .jpg Format

Examples of use

Here will be python Functional language related specifications are stored in a Wordcloud_test.txt Generate word cloud in file

 Functions and methods 

 The functions referred to below , Include function ,  Method ,  And generators .

 A function must have a document string ,  Unless it meets the following conditions :

 Not visible from the outside 

 Very short 

 Simple and clear 

 The document string should contain what functions do ,  And a detailed description of the inputs and outputs .  Usually ,  It should not be described ” How do you do it? ”,  Unless it's a complex algorithm .  There should be enough information in the string ,  When someone else writes code to call the function ,  He doesn't need to look at a line of code ,  Just look at the document string .  For complex code ,  It makes more sense to annotate code than to use document strings .  Subclass methods that override the base class should have a similar  See base class  Simple comments to guide the reader to the document comments of the base class methods . If overloaded subclass methods are very different from base class methods , Then the note should indicate this information .

 Several aspects of the function should be described in specific sections ,  These aspects are described below .  Each section should start with a heading line .  The title line ends with a colon .  Except for the title line ,  The rest of the section should be indented 2 A space .

 List the name of each parameter ,  And use a colon and a space after the name ,  Separate the description of the parameter . If the description is too long than a single line 80 character , Use 2 perhaps 4 Hanging indent of spaces ( Consistent with the rest of the document ).  The description should include the type and meaning required .  If a function accepts *foo( Variable length parameter list ) perhaps **bar ( Any key parameter ),  It should be listed in detail *foo and **bar.

Returns: ( perhaps  Yields:  For generators )
 Describes the type and semantics of the return value .  If the function returns None,  This part can be omitted .

 List all exceptions related to the interface .

The background map is a map of China .
 Insert picture description here

Specific code :

import jieba
import wordcloud
import imageio
mask = imageio.imread("bckg.jpg")
excludes = {}

# Import txt file 
f = open("Wordcloud_test.txt", "r", encoding="utf-8")
t =

ls = jieba.lcut(t)
txt = " ".join(ls)
w = wordcloud.WordCloud(\
    width = 1000, height = 700,\
    background_color = "white",
    font_path = "msyh.ttc", mask = mask

The result is shown in Fig.
 Insert picture description here
Of course, this result seems to have no guiding significance . For learning only .
Reference from Chinese Universities MOOC.

copyright notice
author[Rocky_ ninety-six],Please bring the original link to reprint, thank you.

Random recommended