wordcloud library routine method
import wordcloud c=() #Build word cloud objects, configure object parameters ("wordcloud by python ") #Load word cloud text c.to_file("") #Exporting word cloud files
Word cloud generation steps:
- Separation:Separate words with spaces
- Stats:Word occurrences and filtered (1-2 letters will be filtered out)
- Font:Configuration font size based on statistics
- Layout:Color Environment Size
c=(parameter) Configuration object parameter
Parameters (pixels) | descriptive |
---|---|
width (default 400px) | c=(width=600) |
height (default 200px) | c=(height=400) |
min_font_size (default 4) | c=(min_font_size=10) |
max_font_size (automatically adjusted for height) | c=(max_font_size=20) |
font_step (default 1, step size) | c=(font_step=2) |
font_path (default None, font path) | c=(font_path="") |
max_words (default 200, maximum number) | c=(max_words=20) |
stop_words (specify the list of words to be removed) | c=(stop_words={"python"}) |
background_color (default black, word cloud background color) | c=(background_color="red") |
Scale Default 1 The larger the value, the higher the image density and the sharper it will be
The mask parameter customizes the word cloud shape:
# Specify the shape of the word cloud, default is rectangle, need to quote imread() function from import imread mk=imread("") c=(mask=mk)
third-party repository
- numpy :multidimensional array arithmetic
- matplotlib :Mapping and data visualization
- pillow :Image Processing
- wordcloud :word cloud production
- imageio :Image input and output
- jieba :Chinese participle
- snownlp : Chinese Emotion and Natural Language Processing
- itchat : WeChat Friend Grabber
Read file
Government work report word cloud:
import wordcloud # Read large text from external .txt file into variable txt f = open('Government Work Report.txt',encoding='utf-8') txt = () # Build the word cloud object w, set the word cloud image width, height, font, background color and other parameters w = (width=1000, height=700, background_color='white', font_path='') # Pass the txt variable into w's generate() method to input text to the word cloud (txt) # Export word cloud images to current folder w.to_file('')
== wordcloud Chinese word segmentation (for Chinese word cloud) ==
# Import the wordcloud and jieba libraries import jieba import wordcloud # Build and configure word cloud objects w w = (width=1000, height=700, background_color='white', font_path='') # Call jieba's lcut() method to split the original text into Chinese words and get string txt = 'Due to the simplicity, readability, and extensibility of the Python language, \ There is a growing number of research organizations abroad that use Python for scientific computing, \ Some well-known universities have adopted Python to teach programming courses. \ For example, Carnegie Mellon University's Fundamentals of Programming, \ Introduction to Computer Science and Programming at the Massachusetts Institute of Technology are taught using Python language.' txtlist = (txt) string = " ".join(txtlist) # Pass the string variable into w's generate() method to enter text to the word cloud (string) # (" ".join((txt)))) # three pieces of code in one shorthand # Export word cloud images to current folder w.to_file('')
Customize the drawing of word clouds of a specified shape
import jieba import wordcloud from import imread mask=imread("Picture shape.png") # Import a custom shape image and assign it to a mask. # Read large text from external .txt file into variable txt f = open('Government Work Report.txt',encoding='utf-8') txt = () #Reading Extraction () #Close # Build the word cloud object w, set the word cloud image width, height, font, background color and other parameters w = (width=1000, height=700, background_color='white',mask=mask, font_path='') # Pass the txt variable into w's generate() method to input text to the word cloud (" ".join((txt))) # Export word cloud images to current folder w.to_file('')
==♪ Outlining the word cloud ♪
# Import wordcloud production library wordcloud import wordcloud # Save the text contained in the external file in the string variable string = open('').read() # Import the imread function from the imageio library and use this function to read a local image as a word cloud shape image import imageio mk = ("") # Construct the word cloud object w. Note the addition of the parameters contour_width and contour_color to set the contour width and color. w = (background_color="white", mask=mk, contour_width=1, contour_color='steelblue' ) # # Pass the string variable into w's generate() method to input text to the word cloud (string) # Export word cloud images to current folder w.to_file('')
== Draw word clouds in the template's own colors ==
# Import the plotting library matplotlib and the word cloud production library wordcloud import as plt from wordcloud import WordCloud,ImageColorGenerator # Save the text contained in the external file in the text variable text = open('').read() # Import the imread function from the imageio library and use this function to read a local image as a word cloud shape image import imageio mk = ("") # Construct word cloud objects w wc = WordCloud(background_color="white", mask=mk,) # Pass the text string variable into the generate() method of w to input text to the word cloud (text) # Call the ImageColorGenerator() function in the wordcloud library to extract the color of each part of the template image image_colors = ImageColorGenerator(mk) # Display native word clouds, word clouds colored by template image, and template images by left, center, and right fig, axes = (1, 3) # The leftmost image shows the native word cloud map axes[0].imshow(wc) # The middle image shows the word cloud generated by the template image color, using bilinear interpolation to display the colors axes[1].imshow((color_func=image_colors), interpolation="bilinear") # The image on the right shows the template image axes[2].imshow(mk, cmap=) for ax in axes: ax.set_axis_off() () # Recolor word cloud objects to the color of the template image wc_color = (color_func=image_colors) # Export word cloud images to current folder wc_color.to_file('')
To this point this article on python wordcloud wordcloud custom word cloud production steps to share the article is introduced to this, more related python wordcloud word cloud production content, please search for my previous articles or continue to browse the following related articles I hope you will support me in the future more!