前言:

笔主开发环境:Python3+Windows

推荐初学者使用Anaconda来搭建Python环境,这样很方便而且能提高学习速度与效率。

简介:

wordcloud是Python中的一个小巧的词云生成器。

github:https://github.com/amueller/word_cloud

官网:https://amueller.github.io/word_cloud/

下载:

1——使用conda下载(前提是安装了Anaconda,推荐这种方法):

conda install -c conda-forge wordcloud

2——使用pip命令(笔主开发环境为windows,第一次按这种方法安装,会出现错误,按照网上的解决办法一直没解决):

pip install wordcloud

实例

1–入门案例

#!/usr/bin/env python
"""
Minimal Example
=============== 使用默认参数根据美国宪法生成方形的词云
""" from os import path
from wordcloud import WordCloud d = path.dirname(__file__) # 读取整个文本
text = open(path.join(d, 'constitution.txt')).read() # 生成一个词云图像
wordcloud = WordCloud().generate(text) # matplotlib的方式展示生成的词云图像
import matplotlib.pyplot as plt
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis("off") #max_font_size设定生成词云中的文字最大大小
#width,height,margin可以设置图片属性
# generate 可以对全部文本进行自动分词,但是他对中文支持不好
wordcloud = WordCloud(max_font_size=66).generate(text)
plt.figure()
plt.imshow(wordcloud, interpolation="bilinear")
plt.axis("off")
plt.show() # pil方式展示生成的词云图像(如果你没有matplotlib)
# image = wordcloud.to_image()
# image.show()

2–使用蒙版图像可以生成任意形状的wordcloud。

#!/usr/bin/env python
"""
Masked wordcloud
================ 使用蒙版图像可以生成任意形状的wordcloud。
""" from os import path
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt from wordcloud import WordCloud, STOPWORDS d = path.dirname(__file__) # 读取整个文本.
text = open(path.join(d, 'alice.txt')).read() #读取图片(图片来源:http://www.stencilry.org/stencils/movies/alice%20in%20wonderland/255fk.jpg)
alice_mask = np.array(Image.open(path.join(d, "alice_mask.png"))) stopwords = set(STOPWORDS)
stopwords.add("said")
#设置词云的一些属性
wc = WordCloud(background_color="white", max_words=2000, mask=alice_mask,
stopwords=stopwords)
# 生成词云
wc.generate(text) #保存到本地
wc.to_file(path.join(d, "alice.png")) #展示
plt.imshow(wc, interpolation='bilinear')
plt.axis("off")
plt.figure()
plt.imshow(alice_mask, cmap=plt.cm.gray, interpolation='bilinear')
plt.axis("off")
plt.show()

3–着色

#!/usr/bin/env python
"""
使用自定义颜色
=================== 使用重新着色方法和自定义着色功能。
""" import numpy as np
from PIL import Image
from os import path
import matplotlib.pyplot as plt
import random from wordcloud import WordCloud, STOPWORDS def grey_color_func(word, font_size, position, orientation, random_state=None,
**kwargs):
return "hsl(0, 0%%, %d%%)" % random.randint(60, 100) d = path.dirname(__file__) # 读取图片(图片来源:http://www.stencilry.org/stencils/movies/star%20wars/storm-trooper.gif)
mask = np.array(Image.open(path.join(d, "stormtrooper_mask.png"))) # 文字来源:“新希望”电影剧本(网址:http://www.imsdb.com/scripts/Star-Wars-A-New-Hope.html)
text = open(path.join(d, 'a_new_hope.txt')).read() # 预处理一点点文本
text = text.replace("HAN", "Han")
text = text.replace("LUKE'S", "Luke") # 添加电影剧本特定的停用词
stopwords = set(STOPWORDS)
stopwords.add("int")
stopwords.add("ext") wc = WordCloud(max_words=1000, mask=mask, stopwords=stopwords, margin=10,
random_state=1).generate(text)
# 存储默认的彩色图像
default_colors = wc.to_array()
plt.title("Custom colors")
plt.imshow(wc.recolor(color_func=grey_color_func, random_state=3),
interpolation="bilinear")
wc.to_file("a_new_hope.png")
plt.axis("off")
plt.figure()
plt.title("Default colors")
plt.imshow(default_colors, interpolation="bilinear")
plt.axis("off")
plt.show()

#!/usr/bin/env python
"""
Image-colored wordcloud
=======================
您可以在ImageColorGenerator中实现使用基于图像的着色策略对文字云进行着色,它使用由源图像中的单词占用的区域的平均颜色。 """ from os import path
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt from wordcloud import WordCloud, STOPWORDS, ImageColorGenerator d = path.dirname(__file__) # 读取整个文本
text = open(path.join(d, 'alice.txt')).read() # 读取蒙板/彩色图像(图片是从http://jirkavinse.deviantart.com/art/quot-Real-Life-quot-Alice-282261010下载的)
alice_coloring = np.array(Image.open(path.join(d, "alice_color.png")))
stopwords = set(STOPWORDS)
stopwords.add("said") wc = WordCloud(background_color="white", max_words=2000, mask=alice_coloring,
stopwords=stopwords, max_font_size=40, random_state=42)
# 生成词云
wc.generate(text) # 从图像创建着色
image_colors = ImageColorGenerator(alice_coloring) # 显示
plt.imshow(wc, interpolation="bilinear")
plt.axis("off") #不显示坐标尺寸
plt.figure()
# 重新着色词云并显示
# 我们也可以直接在构造函数中给使用:color_func=image_colors
plt.imshow(wc.recolor(color_func=image_colors), interpolation="bilinear")
plt.axis("off") #不显示坐标尺寸
plt.figure()
plt.imshow(alice_coloring, cmap=plt.cm.gray, interpolation="bilinear")
plt.axis("off") #不显示坐标尺寸
plt.show()#一次绘制三张图



关于着色的另一个例子:

colored_by_group.py

4–表情

 #!/usr/bin/env python
"""
表情实例
===============
一个简单的例子,显示如何包含表情符号。 请注意,这个例子似乎不适用于OS X(苹果系统),但是确实如此
在Ubuntu中正常工作
包含表情符号有3个重要步骤:
1) 使用io.open而不是内置的open来读取文本输入。 这确保它被加载为UTF-8
2) 重写词云使用的正则表达式以将文本解析为单词。 默认表达式只会匹配ascii的单词
3) 将默认字体覆盖为支持表情符号的东西。 包含的Symbola字体包括黑色和白色大多数表情符号的白色轮廓。 目前PIL / Pillow库存在的问题似乎可以预防
它在OS X上运行正常(https://github.com/python-pillow/Pillow/issues/1774)。
如果你有问题,试试在Ubuntu上运行
"""
import io
import string
from os import path
from wordcloud import WordCloud d = path.dirname(__file__) #使用io.open将文件正确加载为UTF-8非常重要
text = io.open(path.join(d, 'happy-emoji.txt')).read() # the regex used to detect words is a combination of normal words, ascii art, and emojis
# 2+ consecutive letters (also include apostrophes), e.x It's
normal_word = r"(?:\w[\w']+)"
# 2+ consecutive punctuations, e.x. :)
ascii_art = r"(?:[{punctuation}][{punctuation}]+)".format(punctuation=string.punctuation)
# a single character that is not alpha_numeric or other ascii printable
emoji = r"(?:[^\s])(?<![\w{ascii_printable}])".format(ascii_printable=string.printable)
regexp = r"{normal_word}|{ascii_art}|{emoji}".format(normal_word=normal_word, ascii_art=ascii_art,
emoji=emoji) # 生成一个词云图片
# Symbola字体包含大多数表情符号
font_path = path.join(d, 'fonts', 'Symbola', 'Symbola.ttf')
wordcloud = WordCloud(font_path=font_path, regexp=regexp).generate(text) # 采用matplotlib方式:展示生成的图片
import matplotlib.pyplot as plt
plt.imshow(wordcloud)
plt.axis("off")
plt.show()

以上所有例子可以到我的github上下载(持续更新Python第三方库使用demo以及常见python爬虫,python玩微信等内容):

https://github.com/Snailclimb/Python/tree/master/PythonDemo/wordcloud

Python第三方库wordcloud(词云)快速入门与进阶的更多相关文章

  1. python爬虫——京东评论、jieba分词、wordcloud词云统计

    接上一章,动态页面抓取——抓取京东评论区内容. url=‘https://club.jd.com/comment/productPageComments.action?callback=fetchJS ...

  2. Python第三方库matplotlib(2D绘图库)入门与进阶

    Matplotlib 一 简介: 二 相关文档: 三 入门与进阶案例 1- 简单图形绘制 2- figure的简单使用 3- 设置坐标轴 4- 设置legend图例 5- 添加注解和绘制点以及在图形上 ...

  3. 数字、字符串、列表、字典,jieba库,wordcloud词云

    一.基本数据类型 什么是数据类型 变量:描述世间万物的事物的属性状态 为了描述世间万物的状态,所以有了数据类型,对数据分类 为什么要对数据分类 针对不同的状态需要不同的数据类型标识 数据类型的分类 二 ...

  4. 【Python基础】安装python第三方库

    pip命令行安装(推荐) 打开cmd命令行 安装需要的第三方库如:pip install numpy 在安装python的相关模块和库时,我们一般使用“pip install  模块名”或者“pyth ...

  5. jieba分词wordcloud词云

    1.jieba库的基本介绍 (1).jieba是优秀的中文分词第三方库 中文文本需要通过分词获得单个的词语 jieba是优秀的中文分词第三方库,需要额外安装 jieba库提供三种分词模式,最简单只需掌 ...

  6. python第三方库requests简单介绍

    一.发送请求与传递参数 简单demo: import requests r = requests.get(url='http://www.itwhy.org') # 最基本的GET请求 print(r ...

  7. Python第三方库资源

    [转载]Python第三方库资源   转自:https://weibo.com/ttarticle/p/show?id=2309404129469920071093 参考:https://github ...

  8. 常用Python第三方库 简介

    如果说强大的标准库奠定了python发展的基石,丰富的第三方库则是python不断发展的保证,随着python的发展一些稳定的第三库被加入到了标准库里面,这里有6000多个第三方库的介绍:点这里或者访 ...

  9. 使用Python第三方库生成二维码

    本文主要介绍两个可用于生成二维码的Python第三方库:MyQR和qrcode. MyQR的使用: 安装: pip install MyQR 导入: from MyQR import myqr imp ...

随机推荐

  1. C语言100例02 PHP版(练习)

    问题: 企业发放的奖金根据利润提成. 利润(I)低于或等于10万元时,奖金可提10%: 利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可提成7.5%: 20万到 ...

  2. 2011 Multi-University Training Contest 4 - Host by SDU

    A.Color the Simple Cycle(polya计数+字符串匹配) 此题的难点在于确定置换的个数,由a[i+k]=a[i], e[i+k]=e[i]联想到KMP. 于是把原串和原串扩大两倍 ...

  3. [BZOJ4822] [CQOI2017] 老C的任务

    题目链接 BZOJ:https://lydsy.com/JudgeOnline/problem.php?id=4822. 洛谷:https://www.luogu.org/problemnew/sho ...

  4. BZOJ2337:[HNOI2011]XOR和路径——题解

    +++++++++++++++++++++++++++++++++++++++++++ +本文作者:luyouqi233. + +欢迎访问我的博客:http://www.cnblogs.com/luy ...

  5. 怎样才能快速成为JavaScript高手

    如何快速成为JavaScript高手?之前我拿这个问题问过我的同事,也问过国内的一些JavaScript高手. 最近,我一直在拿这个问题问自己.之所以会有这个问题,我基于两个前提:第一.我自认为自己不 ...

  6. JavaScript倒计时脚本

    JavaScript倒计时在Web中用得非常广泛,比如常见的团购啊.还有什么值得期待的事情,都可以用到倒计时.现在举了四个例子,比如时间长的倒计时,小时倒计时,最简的倒计时,还有秒表等等,应该可以满足 ...

  7. input file上传图片预览,非插件

    Input标签 <input type="file" name="pic" onchange="changepic(this)" mu ...

  8. Codeforces Round #514 (Div. 2) C. Sequence Transformation(递归)

    C. Sequence Transformation 题目链接:https://codeforces.com/contest/1059/problem/C 题意: 现在有1~n共n个数,然后执行下面操 ...

  9. Sightseeing(dijlstar) 计算最短路和次短路的条数

    Sightseeing Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10004   Accepted: 3523 Desc ...

  10. LoaderManager与CursorLoader用法

    一.基本概念 1.LoaderManager LoaderManager用来负责管理与Activity或者Fragment联系起来的一个或多个Loaders对象. 每个Activity或者Fragme ...