wordcloud word cloud module

wordcloud word cloud module

download

pip install wordcloud

use

import wordcloud ## calls the entire module

form wordcloud import WordCloud ## calls wordcloud in WordCloud sub-module

1. The parameter configuration module

2. Load text .generate ()

3. Output word cloud file .to_file ( "")

from wordcloud import WordCloud
from imageio import imread
import matplotlib.pyplot as plt
import jieba
f = open('中国十九大.txt', encoding="utf-8")#打开文件并转码
date = f.read()#读取文件内容
res = jieba.lcut(date)#将文件内容切成字符串列表
# print(res)
result = "".join(res)#将切成字符串列表后的文件转成文本
# print(result)
f.close()
mask_1 = imread("五角星.jpg")#引入图片模型(面具)使用imageio模块\
#或者使用scipy模块from scipy.misc import imread
wc = WordCloud(background_color="red",
               font_path=r'C:\Windows\Fonts\STFANGSO.TTF',
               width=1960,
               height=1080,
               mask=mask_1)#设置面具
#定义WordCloud参数
wc.generate(result)#向WordCloud中的wc加入转换后的文本
wc.to_file('练习.png')#将转换后的文本存为词云文件
plt.imshow(wc)
plt.show()

Guess you like

Origin www.cnblogs.com/Mr-shen/p/11754602.html