Use the Python word cloud to show the songs sung by Zhou Dong and discover the secrets

Source: Internet
Author: User
Tags set background install matplotlib

You're ready to go, right now.

Preparatory work

Environment: Windows + Python3.6
IDE: Choose your own preference according to your preferences
Module:
Matplotlib is a Python 2D Math drawing Library

1 pip Install matplotlib 2 import Matplotlib.pyplot as Plt

Jieba Chinese word Thesaurus

1 pip Install Jieba 2 Import Jieba

Wordcloud Word Cloud Library

1 pip Install Wordcloud 2  from Import Wordcloud

Actual combat specific steps:

1. Read in txt text data.
2, stuttering Chinese word segmentation, generate string default precision mode, if you do not pass too much words, can not directly generate the correct Chinese word cloud.
3, the generation of word cloud, it is important to note that Wordcloud default does not support Chinese, so here need to have downloaded a good Chinese font.
(No custom background map: You need to specify the pixel size of the generated word cloud, the default background color is black, the unified text color: mode= ' RGBA ' and colormap= ' Pink ')
4. Display the picture.

Overall, there are only four steps that are relatively simple. Let's take a look at the implementation first!

Did you see the secret inside? Full Code
1 #Matplotlib is a Python 2D Math drawing Library2 #Installation: Pip install matplotlib3 ImportMatplotlib.pyplot as Plt4 #Jieba Chinese word thesaurus5 #Installation: Pip install Jieba6 ImportJieba7 #Wordcloud Word Cloud library8 #install pip install Wordcloud9  fromWordcloudImportWordcloudTen  One #1. Read in txt text data AText = open (r'. \jay.txt',"R"). Read () - #print (text) -  the #2. Stutter Chinese participle, generate string, default precision mode, if not through the word, can not directly generate the correct Chinese word cloud -Cut_text =jieba.cut (text) - #print (Type (cut_text)) - #print (Next (cut_text)) + #print (Next (cut_text)) - #print (Next (cut_text)) + #you must separate the result of the word break to form a string, or you cannot draw a cloud of words Aresult =" ". Join (Cut_text) at #print (Result) -  - #3. Generate the word cloud, it is important to note that Wordcloud default does not support Chinese, so it is necessary to download a good Chinese font - #No custom background map: You need to specify the pixel size of the generated word cloud, the default background color is black, the unified text color: mode= ' RGBA ' and colormap= ' Pink ' -WC =Wordcloud ( -         #set the font, without specifying it will appear garbled inFont_path=r'. \simhei.ttf', -         #Set Background color toBackground_color=' White', +         #Set Background width -width=500, the         #Set Background height *height=350, $         #Maximum FontPanax NotoginsengMax_font_size=50, -         #Minimum Font themin_font_size=10, +)#max_words=1000), mode= ' RGBA ', colormap= ' pink ') A #Generate word Clouds the wc.generate (Result) + #Save Picture -Wc.to_file (R". \wordcloud.png")#Save the well-drawn word cloud by setting the pixel width height, clearer than the program shown below $  $ #4. Show Pictures - #specify the drawing name -Plt.figure ("Jay") the #Display the word cloud as a picture - plt.imshow (WC)Wuyi #close the image coordinate system thePlt.axis ("off") - plt.show () Wu Print('Python Learning Exchange Group: 125240963, the group to share the daily dry, including the latest Python enterprise case study materials and 0 basic introductory tutorials, welcome to the group of small partners to learn exchange')

Use the Python word cloud to show the songs sung by Zhou Dong and discover the secrets

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.