您的位置:首页 > 编程语言 > Python开发

python制作不同形状的中文词云

2018-02-25 22:57 253 查看
"""
Masked wordcloud
================
Using a mask you can generate wordclouds in arbitrary shapes.
"""

from PIL import Image
import numpy as np
import matplotlib.pyplot as plt

from wordcloud import WordCloud, STOPWORDS
''' 这里我想去掉数据中的“回复”这个词,因为它属于杂质,直接去掉会报编码错误,
用# -*- coding: UTF-8 -*-不能解决,于是找到这个方法,改变默认的编码方式,并不太懂什么意思'''
import sys
reload(sys)
sys.setdefaultencoding('utf-8')

# Read the whole text.
#text = open(path.join(d, 'alice.txt')).read()

text=open('fzw.txt').read()

# read the mask image
# taken from
# http://www.stencilry.org/stencils/movies/alice%20in%20wonderland/255fk.jpg alice_mask = np.array(Image.open("alice_mask.png"))
#这里添加想要的形状,用png图片
#stopwords = set(STOPWORDS)
#stopwords.add("said")
'''background_color="white"'''
wc = WordCloud( background_color="white",max_words=2000, mask=alice_mask,              font_path="minisong.ttf",max_font_size=40,scale=0.8) #mask参数指定词云形状
# generate word cloud
# generate word cloud
mytext=" ".join(jieba.cut(text))
mytext=mytext.replace('回复',' ')
wc.generate(mytext)

# store to file
wc.to_file("alice.png")

# show
plt.imshow(wc, interpolation='bilinear')
plt.axis("off")
#plt.figure()
#plt.imshow(alice_mask, cmap=plt.cm.gray, interpolation='bilinear')
#plt.axis("off")
plt.show()

# The pil way (if you don't have matplotlib)
# image = wordcloud.to_image()
# image.show()


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: