python词云 wordcloud入门

构建词云的方法很多, 但是个人觉得python的wordcloud包功能最为强大,还可以自定义图片.
官网: https://amueller.github.io/word_cloud/
github: https://github.com/amueller/word_cloud

安装wordcloud
方法1
pip install wordcloud
方法2
github下载并解压
wget  https://github.com/amueller/word_cloud/archive/master.zip
unzip master.zip
rm master.zip
cd word_cloud-master

安装依赖包
sudo pip install -r requirements.txt

安装wordcloud
python setup.py install

与模版图片颜色相同的词云

采用https://amueller.github.io/word_cloud/auto_examples/colored.html#colored-py 进行讲解

#!/usr/bin/python
# -*- coding:UTF-8 -*-
from os import path
from scipy.misc import imread
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
from wordcloud import WordCloud, STOPWORDS, ImageColorGenerator

import sys
reload(sys)
sys.setdefaultencoding('utf-8')

d = path.dirname(__file__)

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

# read the mask / color image
# taken from http://jirkavinse.deviantart.com/art/quot-Real-Life-quot-Alice-282261010
alice_coloring = imread(path.join(d, "alice_color.png"))

wc = WordCloud(background_color="white", max_words=100, mask=alice_coloring,
               stopwords=STOPWORDS.add("said"),
               max_font_size=50, random_state=42)
# generate word cloud
wc.generate(text)

# create coloring from image
image_colors = ImageColorGenerator(alice_coloring)

# show
plt.imshow(wc)
plt.axis("off")
plt.figure()
# recolor wordcloud and show
# we could also give color_func=image_colors directly in the constructor
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()
#save img
wc.to_file(path.join(d, "cloudimg.png"))

cat alice_result.txt

3DS,PS3,PSV,PS4,XBox360,3DSLL,PSP,500G,3dsll,1000,WIIU,3ds,XboxOne,250G,65,PSV2000,4000,2000,8G,WII

参考:
https://zhuanlan.zhihu.com/p/20432734?refer=666666
http://blog.csdn.net/tanzuozhev/article/details/50789226

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值