Python generates a Chinese word cloud image (a): You can specify the shape and color of specific word clouds. __python

Source: Internet
Author: User
python generates Chinese word cloud What is the word cloud.

First, what is the word cloud? The word cloud is also called the text cloud, is to appear the high frequency in the text data "The key word" in the visual prominent present, forms the keyword the rendering to resemble the cloud similar color picture, thus one eye can appreciate the text data the main expression meaning.
This article aims to:

This blog mainly introduces the use of Python Wordcloud package to achieve the generation of Chinese word cloud. You can specify a specific word cloud shape and color.
Python generates English word cloud images see blog python generates English word cloud. Computer Environment Requirements:

Install good pycharm or anaconda. It is recommended to install Pycharm and anaconda at the same time, and Pycharm configured Anaconda, so that you do not have to manually install Wordcloud packets and Jieba word packets, etc., and later do not have to install some other commonly used bags.
Install Pycharm and Anaconda see Using Anaconda in Pycharm. Code implementation:

#coding =utf-8 #Version:p ython3.6.0 #添加自定义分词 import jieba from OS import path #用来获取文档的路径 #词云 from PIL import Image impor T NumPy as NP import Matplotlib.pyplot as PLT #词云生成工具 from Wordcloud import wordcloud,imagecolorgenerator #需要对中文进行处理 impo RT matplotlib.font_manager as FM #背景图 Bg=np.array (Image.open ("tuoyuan.jpg")) #获取当前的项目文件加的路径 D=path.dirname (__file__) # Read Deactivate thesaurus stopwords_path= ' Stopwords.txt ' #添加需要自定以的分词 # jieba.add_word ("Ye Wenjie") #读取要分析的文本 text_path= "Ziranyuyan.txt" # Reads the text to parse, reads the Format text=open (Path.join (D,text_path), encoding= "UTF8"). Read () #定义个函数式用于分词 def jiebacleartext (text): #
    Define an empty list, save the word word that is removed mywordlist=[] #进行分词 seg_list=jieba.cut (text,cut_all=false) #将一个generator的内容用/connection Liststr= '/'. Join (seg_list) #打开停用词表 f_stop=open (stopwords_path,encoding= "UTF8") #读取 Try:f_stop_tex T=f_stop.read () finally:f_stop.close () #关闭资源 #将停用词格式化, separated by \ n, returns a list of F_stop_seg_list=f_stop_text.split (" \ n ") #对默认模式分词的进行遍历, remove the stop word for myworD in Liststr.split ('/'): #去除停用词 if isn't (Myword.split ()) in F_stop_seg_list and Len (Myword.strip ()) >1: Mywordlist.append (MyWord) return '. Join (mywordlist) text1=jiebacleartext (text) #生成 Wc=wordcloud (b 
        Ackground_color= "White", #设置背景为白色, defaults to Black width=990, #设置图片的宽度 height=440, #设置图片的高度 margin=10, #设置图片的边缘 max_font_size=50, random_state=30, font_path= ' C:/windows/fonts/sim Kai.ttf ' #中文处理, with the system's own fonts). Generate (Text1) #为图片设置字体 MY_FONT=FM. Fontproperties (fname= ' C:/windows/fonts/simkai.ttf ') #产生背景图片, color generator based on color image image_colors=imagecolorgenerator (BG) # Start Drawing plt.imshow (WC) #为云图去掉坐标轴 plt.axis ("Off") #画云图, showing #保存云图 wc.to_file ("Tuoyuan3.png")
the generated Chinese word cloud:

reference materials:

1, based on the Python word Cloud Generation (ii)
2, pycharm use of anaconda

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.