词云图
from os import path from PIL import Image import numpy as np import matplotlib.pyplot as plt from wordcloud import WordCloud, STOPWORDS, ImageColorGenerator import jieba text = open(path.join(r‘new.txt‘),encoding=‘utf-8‘).read() wordlist = jieba.cut(text, cut_all=True) txts = " ".join(wordlist) # 这个图片自己选 coloring = np.array(Image.open(path.join("pic.jpg"))) # 你可以通过 mask 参数 来设置词云形状 wc = WordCloud(background_color="white", max_words=2000, mask=coloring,max_font_size=100, random_state=42,font_path=‘fangsong.ttf‘) wc.generate(txts) # create coloring from image image_colors = ImageColorGenerator(coloring) # 在只设置mask的情况下,你将会得到一个拥有图片形状的词云 plt.imshow(wc, interpolation="bilinear") plt.axis("off") plt.figure() plt.show()
效果如图:(右边是随便选择的形状)
原文地址:https://www.cnblogs.com/51python/p/10440534.html
时间: 2024-10-31 21:35:29