python的一个好玩模块wordcloud

python真的超级超级好玩呐,不管是爬虫还是数据挖掘,真的都超级有意思。

今天,来说一说python一个好玩的模块wordcloud

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



字体用的是cabin-sketch.bold


安装

方法1

pip install wordcloud

方法2

github下载并解压

<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">wget  https://github<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.com</span>/amueller/word_cloud/archive/master<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.zip</span>
unzip master<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.zip</span>
rm master<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.zip</span>
cd word_cloud-master</code>

安装依赖包

sudo pip install -r requirements.txt

安装wordcloud

python setup.py install


方法三

下载.whl文件http://www.lfd.uci.edu/~gohlke/pythonlibs/#wordcloud

使用cd命令进入whl文件的路径

运行这条命令:

python -m pip install <filename>








以下是例子

#coding:utf-8
from os import path
from scipy.misc import imread
import matplotlib.pyplot as plt
import jieba

from wordcloud import WordCloud, STOPWORDS, ImageColorGenerator

stopwords = {}
def importStopword(filename=''):
    global stopwords
    f = open(filename, 'r', encoding='utf-8')
    line = f.readline().rstrip()

    while line:
        stopwords.setdefault(line, 0)
        stopwords[line] = 1
        line = f.readline().rstrip()

    f.close()

def processChinese(text):
    seg_generator = jieba.cut(text)  # 使用结巴分词,也可以不使用

    seg_list = [i for i in seg_generator if i not in stopwords]

    seg_list = [i for i in seg_list if i != u' ']

    seg_list = r' '.join(seg_list)

    return seg_list

importStopword(filename='./stopwords.txt')


# 获取当前文件路径
# __file__ 为当前文件, 在ide中运行此行会报错,可改为
# d = path.dirname('.')
d = path.dirname(__file__)


text = open(path.join(d, 'love.txt'),encoding ='utf-8').read()

#如果是中文
#text = processChinese(text)#中文不好分词,使用Jieba分词进行

# read the mask / color image
# taken from http://jirkavinse.deviantart.com/art/quot-Real-Life-quot-Alice-282261010
# 设置背景图片
back_coloring = imread(path.join(d, "./image/love.jpg"))

wc = WordCloud( font_path='./font/cabin-sketch.bold.ttf',#设置字体
                background_color="black", #背景颜色
                max_words=2000,# 词云显示的最大词数
                mask=back_coloring,#设置背景图片
                max_font_size=100, #字体最大值
                random_state=42,
                )
# 生成词云, 可以用generate输入全部文本(中文不好分词),也可以我们计算好词频后使用generate_from_frequencies函数
wc.generate(text)
# wc.generate_from_frequencies(txt_freq)
# txt_freq例子为[('词a', 100),('词b', 90),('词c', 80)]
# 从背景图片生成颜色值
image_colors = ImageColorGenerator(back_coloring)

plt.figure()
# 以下代码显示图片
plt.imshow(wc)
plt.axis("off")
plt.show()
# 绘制词云

# 保存图片
wc.to_file(path.join(d, "名称.png"))

另外附上程序的源码和文件地址:
https://github.com/fyuanfen/wordcloud

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值