This article has participated in the activity of “New person creation Ceremony”, and started the road of digging gold creation together

preface

In previous articles, we have introduced how to use wordcloud library to make Chinese and English wordcloud, and introduced how to use stop words in Chinese and English. However, how to beautify the wordcloud, such as changing font background color, background image, etc., will be introduced in detail in this article.

Parameters,

To beautify the wordcloud, it’s important to understand the meanings of wordcloud’s custom parameters.

Font_path = 'boldface' TTF 'width: Height: int (default=200) # horizontal/prefer_horizontal: Float (default=0.90) # float (default=0.90) # float (default=0.90) # Nd -array or None (default=None) # If the argument is empty, use a 2d mask to draw the word cloud. If mask is not empty, the set width and height value is ignored and the mask shape is replaced by mask. The part that is all white (#FFFFFF) will not be drawn, and the rest will be used to draw the word cloud. For example: bg_pic = imread(' read an image.png '), the canvas of the background image must be set to white (#FFFFFF), and then display a shape other than white. You can use the PHOTOSHOP tool to copy the shape you want to display to a pure white canvas and then save, ok. Min_font_size: int (default=4) # Display the smallest font size font_step: float (default=1) # display the smallest font size font_step: float (default=1) # display the smallest font size Max_words: number (default=200) # max_words: number (default=200) # Set of strings or None # set the words to be masked, if empty, use the built-in STOPWORDS background_color: Color value (default= "black") # background_color='white', background_color= white max_font_size: Int or None (default=None) # display maximum font size mode: String (default= "RGB") # When the parameter is "RGBA" and background_color is not empty, the background is transparent relative_scaling: Float (default=.5) # color_func: callable, default=None # Function to generate a new color, if empty, use self.color_func regexp: Collocations: bool, default=True # collocations: bool, default=True # String or matplotlib colormap, default= "viridis" # assign color to each word randomly, if color_func is specified, then ignore this method random_state: Int or None # Returns a PIL color for each wordCopy the code

Let’s take a simple example, we do Chinese word cloud, we must set Chinese font, otherwise it will be garbled, so we must set font_size; The default background is black, if we want to set it to something else we need to set background_color; Set width and height to 16:9; Of course, to set the stopwords, you need to use stopwords.

From matplotlib import pyplot as PLT from wordcloud import wordcloud import jieba text = 'MY name is Luo Pan, he is Guanyu, I am Luo Pan, Join (jieba.cut(text)) stopwords = set() content = [line.strip() for line in open('hit_stopwords.txt','r').readlines()] stopwords.update(content) wc = WordCloud(background_color = '#F3F3F3', font_path = r'/System/Library/Fonts/Supplemental/Songti.ttc', stopwords = stopwords, width = 1600, Height = 900) wc. Generate (cut_word) plt.imshow(WC, Interpolation ='bilinear') plt.axis('off')Copy the code

Custom background color

Finally, how to set the background color is the most important issue for everyone. In fact, we just use the mask parameter. We also downloaded an image from the Internet.

The from matplotlib import pyplot as PLT from wordcloud import wordcloud, ImageColorGenerator import jieba text = 'I'm Luo Pan, his name is guan yu, My name is Luo Pan, his name is Liu Bei, aka Wang Wu. Join (jieba.cut(text)) stopWords = set() content = [line.strip() for line in open('hit_stopwords.txt','r').readlines()] stopwords.update(content) background_image = plt.imread('love.jpeg') wc = WordCloud(background_color = 'white', font_path = r'/System/Library/Fonts/Supplemental/Songti.ttc', stopwords = stopwords, Mask = background_image) wc.generate(cut_word) # change font color img_colors = ImageColorGenerator(background_image) # Change font color to background image color Wc.recolor (color_func=img_colors) plt.imshow(WC, interpolation='bilinear') plt.axis('off') #Copy the code

We’ll see you next time