python wordcloud的使用

1、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
 
 
  • 1
  • 2
  • 3
  • 4

安装依赖包

sudo pip install -r requirements.txt
 
 
  • 1

安装wordcloud

python setup.py install
出现以下情况:


https://www.microsoft.com/en-us/download/details.aspx?id=44266下载Microsoft Visual C++ Compiler for Python 2.7


安装以下即可,接下来遇到什么安装包没安装的,在cmd环境下直接pip install jieba(工具包名)

#!/usr/bin/env python
#-*-coding:utf-8-*-
import jieba
from wordcloud import WordCloud
import matplotlib.pyplot as plt
 
s1 = """ 在克鲁伊夫时代,巴萨联赛中完成了四连冠,后三个冠军都是在末轮逆袭获得的。
在91/92赛季,巴萨末轮前落后皇马1分,结果皇马客场不敌特内里费使得巴萨逆转。
一年之后,巴萨用几乎相同的方式逆袭,皇马还是末轮输给了特内里费。
在93/94赛季中,巴萨末轮前落后拉科1分。
巴萨末轮5比2屠杀塞维利亚,拉科则0比0战平瓦伦西亚,巴萨最终在积分相同的情况下靠直接交锋时的战绩优势夺冠。
神奇的是,拉科球员久基奇在终场前踢丢点球,这才有了巴萨的逆袭。"""
 
s2 = """ 巴萨上一次压哨夺冠,发生在09/10赛季中。末轮前巴萨领先皇马1分,只要赢球就将夺冠。
末轮中巴萨4比0大胜巴拉多利德,皇马则与对手踢平。
巴萨以99分的佳绩创下五大联赛积分纪录,皇马则以96分成为了悲情的史上最强亚军。"""
 
s3 = """在48/49赛季中,巴萨末轮2比1拿下同城死敌西班牙人,以2分优势夺冠。
52/53赛季,巴萨末轮3比0战胜毕巴,以2分优势力压瓦伦西亚夺冠。
在59/60赛季,巴萨末轮5比0大胜萨拉戈萨。皇马巴萨积分相同,巴萨靠直接交锋时的战绩优势夺冠。"""
 
mylist = [s1, s2, s3]
word_list = [" ".join(jieba.cut(sentence)) for sentence in mylist]
new_text = ' '.join(word_list)
wordcloud = WordCloud(font_path="D:\\Users\\Administrator\\Pythoncode\\wordcloud\\MSYH.TTF", background_color="black").generate(new_text)
plt.imshow(wordcloud)
plt.axis("off")
plt.show()

结果如下:

需要下载一下MSYH.TTF字体:网址http://www.121down.com/soft/softview-31524.html


from os import path
from scipy.misc import imread
import matplotlib.pyplot as plt


from wordcloud import WordCloud, STOPWORDS, ImageColorGenerator
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
# 获取当前文件路径
# __file__ 为当前文件, 在ide中运行此行会报错,可改为
# d = path.dirname('.')
d = path.dirname(__file__)


# 读取文本 alice.txt 在包文件的example目录下
#内容为


"""
Project Gutenberg's Alice's Adventures in Wonderland, by Lewis Carroll


This eBook is for the use of anyone anywhere at no cost and with
almost no restrictions whatsoever.  You may copy it, give it away or
re-use it under the terms of the Project Gutenberg License included
with this eBook or online at www.gutenberg.org
'
"""


text = open(path.join(d, 'alice.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, "2222.png"))


wc = WordCloud(background_color="white", #背景颜色max_words=2000,# 词云显示的最大词数
mask=alice_coloring,#设置背景图片
stopwords=STOPWORDS.add("said"),
max_font_size=40, #字体最大值
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(alice_coloring)


# 以下代码显示图片
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()
# 保存图片
wc.to_file(path.join(d, "名称.png"))

程序中有可能出现这种情况:UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position 108:


解决方法是在文件开头加上:

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


  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

samoyan

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值