Use python to implement personalized word cloud, and use python to personalize word cloud

Source: Internet
Author: User
Tags install matplotlib

Use python to implement personalized word cloud, and use python to personalize word cloud

First slice


Word cloud

Template required

pip install jiebapip install wordcloud

I also need to install two other things. I don't know much about them either.

pip install scipypip install matplotlib

Because ubuntu is not as troublesome as windows, and there are not so many errors.

I can see that many people are excited about making their own word cloud. If I am excited, I will do it right away. Impulse is the first creativity.

Jieba is a very hateful Chinese Word Segmentation template.

Jeba Chinese Document

As for wordcloud, there are no Chinese documents, but we can guess it. If it is not good at English, we can guess it and check the source code.

Contribute all my code first

#-*-Coding: UTF-8 -- *-import jieba. analysefrom wordcloud import WordCloud, ImageColorGeneratorfrom scipy. misc import imreadimport matplotlib. pyplot as pltclass wc: def _ init _ (self, txt_file, img_file, font_file): self. f = open (txt_file, 'R') self.txt = self. f. read () self. f. close () self. tags = jieba.analyse.extract_tags(self.txt, topK = 100) # The topK returns several keywords self. text = ''. join (self. tags) # link the word segmentation, and add spaces because English relies on Space word segmentation self. img = imread (img_file) self. wc = WordCloud (font_path = font_file, background_color = 'white', max_words = 100, mask = self. img, max_font_size = 80) ### click here to guess #### font_path indicates the font file path, because the fonts provided by wordcloud do not support Chinese characters, We need to specify a font file, otherwise, all output images are framed. # background_color: Black by default. I set it to white. # max_words: Maximum number of words displayed. # mask background image # max_font_size: Maximum font size. self. word_cloud = self. wc. generate (self. text) def show_wc (self): # img_color = ImageColorGenerator (self. img) plt. imshow (self. word_cloud) # You can use plt. imshow (self. wc. recolor (color_func = img_color) to make the image color the same as the font color plt. axis ("off") plt. show () if _ name __= = '_ main _': mywc = wc('sanwen.txt', 'out.png ', 'font. ttc ') mywc. show_wc ()


Use wc. recolor (color_func = img_color)

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.