I believe you are no stranger to the use of word cloud. It's easy to use. Just call wordcloud package directly. Its main function is to generate pictures according to the text vocabulary and vocabulary frequency, from which we can directly see the proportion of each vocabulary. Recently, I just wanted to make a person's logo, so I decided to use word cloud to make it.
Basic environment configuration
- Version: Python 3.6
- System: Windows
- Related modules: wordcloud, PIL, numpy
- Module installation: pip install wordcloud, PIP install pilot, pip install numpy
You will definitely encounter a pit when using pip to install. During the installation process, an error may be reported, prompting you to install Microsoft Visual C++ 14.0, but this installation process is time-consuming.
You can go to Baidu online to download this environment
We will start to make our pictures as soon as the installation is successful.
1 from os import path 2 from PIL import Image 3 import numpy as np 4 import matplotlib.pyplot as plt 5 6 from wordcloud import WordCloud, STOPWORDS, ImageColorGenerator 7 8 ''' 9 Want to learn Python？Python Learning exchange group: 984632579 to meet your needs, information has been uploaded to the group file, you can download! 10 ''' 11 12 d = path.dirname(__file__) 13 14 # Read text file 15 text = open(path.join(d, 'data.txt')).read() 16 17 # Read custom picture 18 alice_coloring = np.array(Image.open(path.join(d, "pic.jpg"))) 19 20 # You can go through mask Parameter to set word cloud shape 21 wc = WordCloud(background_color="white",max_words=2000, 22 mask=alice_coloring, max_font_size=60,random_state=102,scale=8, 23 font_path="C:\Windows\Fonts\msyhbd.ttf").generate(text) 24 25 wc.generate_from_text(text) 26 print('Start loading text') 27 # Change font color 28 img_colors = ImageColorGenerator(alice_coloring) 29 # Font color is the color of the background picture 30 wc.recolor(color_func=img_colors) 31 # Show word cloud 32 plt.imshow(wc, interpolation="bilinear") 33 # Whether to display x Shaft, y Axis subscript 34 plt.axis('off') 35 plt.show() 36 # Get the path of the module 37 d = path.dirname(__file__) 38 # Return after combining multiple paths 39 wc.to_file(path.join(d, "h16.jpg")) 40 print('Successfully generated word cloud!')
Configuring the parameters of wordcloud is particularly important for image effect. Let's focus on the meaning of the parameters of wordcloud:
This is all the content that Xiaobian has shared with you. I hope to give you a reference, and I also hope you can have more Xiaobian