# -*- coding: utf-8 -*- #author:卜灵 from wordcloud import WordCloud import jieba import matplotlib.pyplot as plt
def draw_wordcloud(text): comment_text = open(text).read() cut_text = " ".join(jieba.cut(comment_text)) cloud = WordCloud( font_path="chinese.ttf", background_color='white', mask=plt.imread("fu.jpg"), max_words=1000, max_font_size=100, random_state = 30 ) word_cloud = cloud.generate(cut_text) # 产生词云 word_cloud.to_file("wordcloud.jpg") #保存图片 # 显示词云图片 plt.imshow(word_cloud) plt.axis('off') plt.show() word_cloud.to_file("wordcloud.jpg") #保存图片 if __name__ == '__main__': draw_wordcloud(u'西游记.txt')
需要的各种模块就自己下载安装,还需要准备文件:
.jpg(背景图片)
.txt(需要生成词云的文章)
.ttf(网上down一个中文的字体)
效果图:
背景是女盆友照片,嗯,先酱!
http://blog.csdn.net/vivian_ll/article/details/68067574
https://zhuanlan.zhihu.com/p/20436581?refer=666666 Python word_cloud 部分文档翻译 标签云系列(二)
http://www.cnblogs.com/lyrichu/p/6151265.html python wordcloud 对电影《我不是潘金莲》制作词云
https://zhuanlan.zhihu.com/p/20436642