Python word cloud gallery --wordcloud

(1 Introduction

  wordcloud word cloud is an excellent showcase third-party libraries, wordcloud can be a piece of text into a word cloud.

  Word cloud is to words by graphic visual way to show out, intuitive and art. Word cloud often see in our lives, whether it is Chinese or English word cloud word cloud.

  Pip install third-party libraries need to use tools, run the installation command (not IDLE) in the command line. Note: You need to Python Scripts directory under the directory and its directory to the environment variable .

  Use the command pip install wordcloud install third-party libraries, you will be prompted successfully installed after installation, inform the installation was successful.

(2) Description Use

  The library wordcloud word cloud as a WordCloud objects that wordcloud.WordCloud () is a text on behalf of the corresponding object word cloud, a cloud is a word WordCloud object. wordcloud library can draw a word cloud according to a number of parameters like the frequency word appears in the text, when drawing a word cloud, the word cloud shapes, sizes, colors can include fonts are set.

  wordcloud libraries have a basic idea of drawing a specific word cloud, the idea is to use wordcloud library WordCloud objects to indicate a word cloud, WordCloud object is the basis for a word cloud, then the configuration parameters to this object, load the text, output to a file. This is also a step word cloud drawn: 1 configuration object parameter; text word cloud loading 2; 3 output file word cloud. . To complete the two steps, we need to WordCloud object is assigned to a variable name, such that the two steps can be called, for example: w = wordcloud.WordCloud ().

  (Using a comma between parameter) configuration object parameters:

parameter description
width

Specify the target word cloud generated image width, 400 pixels by default, for example:

>>>w=wordcloud.WordCloud(width=600)

height

Specify the target word cloud generated height of the image, 200 pixels by default, for example:

>>>w=wordcloud.WordCloud(height=400)

min_font_size

Specifies the minimum word cloud font size, the default number 4, for example:

>>>w=wordcloud.WordCloud(min_font_size=10)

max_font_size

Specifies the maximum size of the font in a word cloud, automatically adjusted according to the height, for example:

>>>w=wordcloud.WordCloud(max_font_size=20)

font_step

Step interval specified word cloud font size, the default is 1

>>>w=wordcloud.WordCloud(font_step=2)

font_path

Specifies the path to the font file, the default is None

>>>w=wordcloud.WordCloud(font_path="msyh.ttc")

max_words

Specifies the maximum number of word cloud shows words, the default 200, for example:

>>>w=wordcloud.WordCloud(max_words=20)

stop_words

Specified word cloud list of excluded words, that the word list is not displayed, for example:

>>>w=wordcloud.WordCloud(stop_words={"Python"})

mask

Specifies a word cloud shape, a rectangular default, need to reference imread () function is in use, for example:

>>>from scipy.misc import imread

>>>mk=imread("pic.png")

>>>w=wordcloud.WordCloud(mask=mk)

background_color

Specifies the background color word cloud pictures, the default is black, for example:

>>>w=wordcloud.WordCloud(background_color="white")

  Load word cloud text method (default premise w = wordcolud.WordCloud ()):

method description
w.generate(txt)

Txt to load text objects WordCloud w, for example:

>>>w.generate("Python and WordCloud")

 

  Output word cloud file method (default premise w = wordcolud.WordCloud ()):

method description
w.to_file(filename)

The word cloud output image file, a .jpg or .png employed, for example:

>>>w.to_file("outfile.png")

  When not specify the image width and height, the default image width of 400 pixels, 200 pixels high.

  The text into a word cloud, wordcloud library probably do four things: 1.wordcloud library with a space as a separator, dividing the text into words; 2.wordcloud library will count the number of times each word appears in the text, the word appears the more times, then the larger the word the word cloud effect display font, and vice versa reversed. And the only word 1-2 characters filtered out; 3.wordcloud library statistics based on the number of times the word appears, configure the font size for the display of different words; 4 layout.

  English word cloud instance:

import wordcloud
txt="life is short,you need python"
w=wordcloud.WordCloud( \
    background_color="white")
w.generate(txt)
w.to_file("pywcloud.png")

  Generated pictures

  Chinese word cloud instance:

import jieba
import wordcloud
txt="程序设计语言是计算机能够理解和\
识别用户操作意图的一种交互体系,它按照\
特定规则组织计算机指令,使计算机能够自\
动进行各种运算处理"
w=wordcloud.WordCloud( width=1000,\
    font_path="msyh.ttf",height=700)  #必须设置字体,否则中文会显示成方框。这里字体文件与.py文件处于同一目录下
w.generate(" ".join(jieba.lcut(txt)))
w.to_file("pywcloud.png")

  生成的图片

Guess you like

Origin www.cnblogs.com/diantong/p/12654331.html