1.安装WordCloud
pip install wordcloud
2.导入
import pickle
import jieba
import pandas as pd
import wordcloud
import matplotlib.pyplot as plt
from imageio import imread
#%%
#读取chapter
pickle_file = open(r'C:\Users\yandi\PycharmProjects\MachineLearing\LearningTest01\SDTest\chapter.pkl','rb')
chapter = pickle.load(pickle_file)
pickle_file.close()
#%%
#读取停用词
stoplist = list(pd.read_csv(r'C:\Users\yandi\PycharmProjects\MachineLearing\LearningTest01\停用词.txt',
names=['w'],sep='aaa',encoding='UTF-8',engine='python').w)
def m_cut(intxt):
return [w for w in jieba.cut(intxt) if w not in stoplist and len(w) > 1]
ls = " ".join(m_cut(chapter.txt[1]))
#%%
cloudobj = wordcloud.WordCloud(mask=imread(r'C:\Users\yandi\PycharmProjects\MachineLearing\LearningTest01\射雕背景0.jpg'),
mode='RGBA',background_color=None
).generate(ls)
#%%
plt.imshow(cloudobj)
plt.axis('off')
plt.show()
- ls是字符串,但是传到WordCloud里面必须是用空格间隔的
- mask是指定背景图片
- 指定图片的色系