[Python word cloud] When you are bored, let's make a word cloud of your own

Foreword:
I feel that word cloud is quite fun without contact with Python, so now I have just finished learning word cloud production, so I will share with you a wave. English word cloud production is relatively simple, so I will directly share the Chinese word cloud production method and complete code here. Pick up at the end.
1. Effect display
Please add image description

Production steps
1. First import the relevant libraries that we need to depend on

import jieba
import wordcloud
import matplotlib.pyplot as plt
import imageio

2. Define our word cloud background image (the darker the color contrast, the better, preferably a solid color)

#背景图片可以自行上网上搜索
#可以使用绝对路径也可以使用相对路径,建议使用相对路径
mask = imageio.imread(r'#图片路径')

3. Specify the text position and style of play

#可以使用绝对路径也可以使用相对路径,建议使用相对路径
with open('文本位置','r',encoding='utf-8')as f:
    words = f.read()

4. Tokenize the text

#对中文进行分词,不然词云显示会变为一个个的句子。
string = words
#利用jieba库对指定文本进行分词
seg = jieba.cut(string,cut_all=False)
#对分词需求进行定义,根据需求进行自行调整。
word_list = [word for word in seg if len(word.strip())>1]
str = '.'.join(word_list)

5. Adjust the font size, color and number of words in the word cloud

#指定模板
wcloud = wordcloud.WordCloud(
    font_path='./zt.ttf', #字体路径
    background_color='white', #指定背景颜色
    max_words=500,   #词云显示最大词数
    max_font_size=150,  #指定最大字号
    mask = mask #背景图片
) 

6. Generate and display word cloud

wcloud = wcloud.generate(str)  #生成词云
plt.imshow(wcloud)
plt.savefig('eng-map.jpg')
plt.axis('off')
plt.show()

Three, word cloud complete code


# coding=gbk
import jieba
import wordcloud
import matplotlib.pyplot as plt
import imageio

mask = imageio.imread(r'./china.png')
with open('./zgg.txt','r',encoding='utf-8')as f:
    words = f.read()
string = words
seg = jieba.cut(string,cut_all=False)
word_list = [word for word in seg if len(word.strip())>1]
str = '.'.join(word_list)
wcloud = wordcloud.WordCloud(
    font_path='./zt.ttf', #字体路径
    background_color='white', #指定背景颜色
    max_words=500,   #词云显示最大词数
    max_font_size=150,  #指定最大字号
    mask = mask #背景图片
) #指定模板
wcloud = wcloud.generate(str)  #生成词云
#避免中文乱码
plt.imshow(wcloud)
plt.savefig('eng-map.jpg')
plt.axis('off')
plt.show()

Guess you like

Origin blog.csdn.net/xiaobai729/article/details/124428720