在python 3.6环境中 使用wordcloud模块制作词云,首先在安装wordcloud模块,安装方法有几个,可参考安装方法与可能会遇到的问题
代码:
"""
@author: gwu
@software: PyCharm
@time: 2017/3/7 0007 18:11
"""
from os
import path
from scipy.misc
import imread
import matplotlib.pyplot
as plt
from wordcloud
import WordCloud, STOPWORDS, ImageColorGenerator
__basePath =
'../../data/'
__fileNamePath =
u'../../data/wordcloud/love.txt'
__stopWordPath =
u'../../data/dict/stopword/stopwords.txt'
__imagePath =
u'../../data/image/alice_color.png'
__ttfPath =
u'../../data/font/李旭科漫画体v1.0.ttf'
if __name__ ==
'__main__':
d = path.dirname(__file__)
text = open(path.join(d, __fileNamePath)).read()
alice_coloring = imread(path.join(d, __imagePath))
wc = WordCloud(background_color=
"black", max_words=
2000, mask=alice_coloring,
stopwords=STOPWORDS.add(
"said"),
max_font_size=
40, random_state=
42)
wc.generate(text)
image_colors = ImageColorGenerator(alice_coloring)
plt.imshow(wc)
plt.axis(
"off")
plt.figure()
plt.imshow(wc.recolor(color_func=image_colors))
plt.axis(
"off")
plt.figure()
plt.imshow(alice_coloring, cmap=plt.cm.gray)
plt.axis(
"off")
plt.show()
结果示例:
转载请注明原文地址: https://ju.6miu.com/read-1683.html