码迷,mamicode.com
首页 > 编程语言 > 详细

python词云

时间:2019-02-26 22:25:16      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:div   情况下   off   ati   show   自己   mat   axis   效果   

词云图

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(rnew.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()

 

效果如图:(右边是随便选择的形状)

技术图片技术图片

 

 

 

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                

python词云

标签:div   情况下   off   ati   show   自己   mat   axis   效果   

原文地址:https://www.cnblogs.com/51python/p/10440534.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!