Today, it’s 520.
It is also known as the “day of love”.
Love makes the world go round!
Coding (Sheng FA) programmers
Struggling with how to express? !
That’s okay. We’ve got you covered!
Today, some core programmer skills
Create your own word cloud in Python!
🎵 (listen to a song ~ let us take a happy mood, together to prepare “confession” it!
Officially Missing You Sabrina – I Love Acoustic Greatest Hits
Making a word cloud is not as difficult as you might think.
Whether you’ve learned Python or not, trust me! Be the school of arts living, also can complete the operation according to the following steps!
01
Environment to prepare
Software environment: Anaconda3.5(download the link to reply to Anaconda) System: macOS Text: jdcdeve.txt
Images: heart. JPG
Main packages: Jieba, WordCloud
☞ open Anaconda, then select Jupyter:
☞ Click the New button to create a New Notebook. Select Python3 from the Notebook.
☞. To install wordCloud and Jieba libraries, enter the following code on the command line:
1pip install wordcloud2pip install jieba
Copy the code
02
The code analysis
Next, you can enter the following code to generate the word cloud:
1 from os import path
2 from PIL import Image
3 import numpy as np
4 import matplotlib.pyplot as plt
5 import os
6 import chardet
7 import jieba
8
9
10from wordcloud import WordCloud, STOPWORDS
11from pip._vendor.pyparsing import WordStart
12
1314d = path.dirname(__file__) if "__file__" in locals() else os.getcwd()
15fontpath='SourceHanSerifK-Light.otf'16, 17Read the text to be parsed
18text = open(path.join(d, 'jdcdeve.txt'), 'r', encoding='UTF-8').read()
19words = jieba.lcut(text)
20cuted = ' '.join(words)
21
2223heart_mask = np.array(image.open (path.join(d, "heart.jpg")))
24
25stopwords = set(STOPWORDS)
26
27wc = WordCloud(font_path=fontpath, background_color="white", max_words=10000, mask=heart_mask,stopwords=stopwords, colormap = "RdBu"28, 29)# Generate word clouds
30wc.generate(cuted)
31
32# save file
33wc.to_file(path.join(d, "result.png"34, 35)# Draw cloud, display 36plt.imshow(WC, Interpolation =' Bilinear ')
37plt.axis("off")
38plt.figure()
39plt.imshow(heart_mask, cmap=plt.cm.gray, interpolation='bilinear')
40plt.axis("off")
41plt.show()
Copy the code
Shift + Enter to see the result.
(note: the font color replacement may refer to http://www.sthda.com/english/wiki/word-cloud-generator-in-r-one-killer-function-to-do-everything-you-need)
03
Results output
If you want to change the text in the generated picture into your beloved or memories, just type the text you want to present in the initial text (such as jdcdeve.txt) and it will appear in the final generated picture!
At last!
In this special day, we also want to tell you a wave!
Thanks to the developers of “JINGdong cloud developer community” has been supporting!
Hope everyone can be loved, and have a lover!!
X.O.X.O~
Welcome to click on the “link” to learn more about jingdong Cloud content
👼 👼 👼
If you’re not there yet, that’s okay!
Make them happy!
Left left left
World smile day | we are! Programmers who can make you laugh until you can’t shut your mouth
Read the original