python词云
from os import path
import jieba
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image
from wordcloud import WordCloud, STOPWORDS, ImageColorGenerator
d = path.dirname(__file__)
f = open('test.txt', 'r', encoding='UTF-8').read()
wordlist = " ".join(jieba.cut(f, cut_all=True));
cloud_image = np.array(Image.open(path.join(d, "girl3.jpg")))
stopwords = set(STOPWORDS)
stopwords.add("said")
wordcloud = WordCloud(background_color="white",
mask=cloud_image,
font_path="仿宋_GB2312.ttf",
max_font_size=20,
random_state=12
)
wordcloud.generate(wordlist)
image_colors = ImageColorGenerator(cloud_image)
plt.imshow(wordcloud.recolor(color_func=image_colors), interpolation="bilinear")
plt.axis("off")
plt.figure()
plt.show()
wordcloud.to_file('test.png')
了解更多请关注微信公众【摩登猪头】,和作者互动,答疑等。公众号上准备了大量的资源等你来领取。你的关注是作者创作的动力。