Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to make Personalized word Cloud Picture by Python

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you Python how to make personalized word cloud map, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

1. Introduction

The word cloud map allows us to easily identify the keywords in the text, in which the size of the words represents their frequency. With this, we can understand the content of the text well even before we read it. Although there are many free tools to create text clouds online, we can use the omnipotent Python to customize personalized word cloud images.

In this article, we will use the third-party Python library stylecloud, with which you can create beautiful word cloud maps with a few lines of code. As follows:

two。 Take a chestnut.

Next, the text we will use to make the word map is part of the speech given by idol Steve Jobs at Stanford University. Click here to get the corresponding .txt file or use any other text to create your own word cloud map.

2.1 install the stylecold library

Here we can directly use pip to install the word library. The code is as follows:

Pip3 install stylecloud2.2 generates word cloud map

Then we can use the stylecloud.gen_stylecloud () method to generate the word cloud map by passing the path of the corresponding text .txt file and the icon style of the word cloud.

On the website, we can easily find a list of icon styles available for stylecloud. In this example, I chose an apple as the icon. The code is as follows:

Import stylecloudstylecloud.gen_stylecloud (file_path='SJ-Speech.txt', icon_name= "fas fa-apple-alt")

After the above code runs, a cloud image of saved words in png format is generated in the current python file directory, as shown below:

2.3 beautify the display effect

If we carefully observe the relevant parameters of the gen_stylecloud function, we can control the background color, the color of the word, the name of the output file, and so on. To do this, let's look at the following code:

Stylecloud.gen_stylecloud (file_path='SJ-Speech.txt', icon_name='fas fa-apple-alt', colors='white', background_color='black', output_name='apple.png', collocations=False)

The running results are as follows:

2.4 processing stop words

We can use the stop_words library to handle the deactivated words in the text, and we can use pip install stop_words to install the library. With the list of disabled words, we can also pass it to the custom_stopwords parameter in the gen_stylecloud function.

The sample code is as follows:

From stop_words import get_stop_wordsstop_words = get_stop_words ('english') stylecloud.gen_stylecloud (file_path='SJ-Speech.txt', icon_name='fas fa-apple-alt', palette='cartocolors.qualitative.Pastel_3', background_color='black', output_name='apple.png' Collocations=False, custom_stopwords=stop_words)

The running result of the above code is as follows:

2.5 use a custom background image

There are hundreds of free icons available for stylecloud on the above sites, but sometimes we may want to use our own images to create more personalized word maps. At this point, we can use the PIL library to read the image, use matplotlib to draw our image, and use wordcloud to make the corresponding word cloud map.

The following code uses the bat pattern to generate the corresponding word cloud image, the code is as follows:

From wordcloud import WordCloud, ImageColorGeneratorfrom PIL import Imageimport matplotlib.pyplot as pltimport numpy as np# create a mask based on the image we wish to includemy_mask = np.array (Image.open ('batman-logo.png')) # create a wordcloudwc = WordCloud (background_color='white', mask=my_mask, collocations=False, width=600, height=300, contour_width=3 Contour_color='black', stopwords=stop_words) with open ('SJ-Speech.txt',encoding='gb18030',errors='ignore') as txt_file: texto = txt_file.read () wc.generate (texto) image_colors = ImageColorGenerator (my_mask) wc.recolor (color_func=image_colors) plt.figure (figsize= (20,10) plt.imshow (wc) Interpolation='bilinear') plt.axis ('off') wc.to_file (' wordcloud2.png') plt.show ()

The running results are as follows:

The above is all the content of the article "how to make personalized word cloud pictures by Python". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report