11.3

Python生成词云图

from wordcloud import WordCloud
import PIL.Image as image
import numpy as np
with open("/home/ly/Desktop/file.txt", encoding='utf-8') as f: #读取文本
text = f.read()
img = np.array(image.open("/home/ly/Downloads/4f8fd0b583bb50d0f05ea791d3b0916b.jpg")) #读取参考图
word_cloud = WordCloud(width=800, #词云图宽
height=600, #词云图高
background_color='white', #词云图背景颜色
mask=img).generate(text) #词云图的参考图
word_cloud_picture = word_cloud.to_image() #生成词云图
word_cloud_picture.show()

原文地址:https://www.cnblogs.com/zhaoyids/p/14867216.html