Python制作词云的方法


Posted in Python onJanuary 03, 2018

需求:

看到朋友圈有人发词云照片,感觉自己也可以玩一玩,于是乎借助wordcloud实现功能。

环境:

MacOS 10.12 +Python 2.7 +Wordcloud
Windows通用

准备:

安装wordcloud

$ pip install wordcloud

SIP功能是Apple在OSX上推出的系统完整性保护功能,新版本的macOS直接用pip安装报错,在不关闭SIP功能的前提下,可以使用

$ pip install wordcloud --user -U

某些情况还会提示错误,需要安装VS for Python,直接上官网下载安装即可。

实现:

源码

#! /usr/bin/env python

# import
from os import path
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
from wordcloud import WordCloud, STOPWORDS

# current path
d = path.dirname(__file__)

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

# read the mask image
test_mask = np.array(Image.open(path.join(d, "test_mask.png")))

stopwords = set(STOPWORDS)
stopwords.add("said")

# setting
wc = WordCloud(background_color="black", max_words=2000, mask=test_mask,
        stopwords=stopwords)

# generate word cloud
wc.generate(text)

# plot and show
plt.imshow(wc, interpolation='bilinear')
plt.axis("off")
plt.figure()
plt.imshow(test_mask, cmap=plt.cm.gray, interpolation='bilinear')
plt.axis("off")
plt.show()

# store to file
wc.to_file(path.join(d, "test.png"))

辅助文件

test_mask.png

Python制作词云的方法

test.txt

The list of big's prior run-ins with the far-right fringe is long and varied. He tweeted fake crime statistics spread by racists to paint black cans as violent, then defended them as credible. He sparked a firestorm last year when he declined to renounce

效果图

Python制作词云的方法

其它说明:

1.文档可以是任意英文txt文件,以上的是网络上英文新闻中的一小段,仅起示例作用。
2.关于中文支持,有多种方法,主要就是分词的问题,这里不讨论了。
3.我不清楚许多人说的定制是什么意思,因为如果想要自己任意想要的形状的话,我觉得用PS做一个png图可以达到同样的效果,经测试也没有发现问题。
4.写的第一篇文章,不足之处欢迎来喷,毕竟我是要学习的。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python中使用动态变量名的方法
May 06 Python
python中对list去重的多种方法
Sep 18 Python
详解Python3.1版本带来的核心变化
Apr 07 Python
Python中random模块用法实例分析
May 19 Python
python绘制评估优化算法性能的测试函数
Jun 25 Python
Django框架 查询Extra功能实现解析
Sep 04 Python
Python箱型图绘制与特征值获取过程解析
Oct 22 Python
使用python创建Excel工作簿及工作表过程图解
May 27 Python
Python return语句如何实现结果返回调用
Oct 15 Python
Django通过设置CORS解决跨域问题
Nov 26 Python
详解Python魔法方法之描述符类
May 26 Python
Python实现PIL图像处理库绘制国际象棋棋盘
Jul 16 Python
Python读取Json字典写入Excel表格的方法
Jan 03 #Python
python基于ID3思想的决策树
Jan 03 #Python
python遍历文件夹下所有excel文件
Jan 03 #Python
Python将多份excel表格整理成一份表格
Jan 03 #Python
Python将多个excel文件合并为一个文件
Jan 03 #Python
python中的字典操作及字典函数
Jan 03 #Python
Python将多个excel表格合并为一个表格
Feb 22 #Python
You might like
PHP5.2下preg_replace函数的问题
2015/05/08 PHP
php微信开发之上传临时素材
2016/06/24 PHP
Laravel手动分页实现方法详解
2016/10/09 PHP
PHP数据源架构模式之表入口模式实例分析
2020/01/23 PHP
父节点获取子节点的字符串示例代码
2014/02/26 Javascript
浅谈javascript原型链与继承
2015/07/13 Javascript
jQuery技巧之让任何组件都支持类似DOM的事件管理
2016/04/05 Javascript
JS控制静态页面传递参数并获取参数应用
2016/08/10 Javascript
JS打开摄像头并截图上传示例
2017/02/18 Javascript
使用jQuery实现一个类似GridView的编辑,更新,取消和删除的功能
2017/03/15 Javascript
深入理解Angular中的依赖注入
2017/06/26 Javascript
IE9 elementUI文件上传的问题解决
2018/10/17 Javascript
javascript实现图片轮换动作方法
2020/08/07 Javascript
[44:50]DOTA2上海特级锦标赛B组小组赛#2 VG VS Fnatic第二局
2016/02/26 DOTA
[00:37]DOTA2上海特级锦标赛 Secert 战队宣传片
2016/03/03 DOTA
python3使用urllib示例取googletranslate(谷歌翻译)
2014/01/23 Python
Django学习教程之静态文件的调用详解
2018/05/08 Python
Python pymongo模块常用操作分析
2018/09/01 Python
Python实现的大数据分析操作系统日志功能示例
2019/02/11 Python
浅析Python 读取图像文件的性能对比
2019/03/07 Python
python 使用while写猜年龄小游戏过程解析
2019/10/07 Python
部署Django到阿里云服务器教程示例
2020/06/03 Python
Jmeter调用Python脚本实现参数互相传递的实现
2021/01/22 Python
python 如何读、写、解析CSV文件
2021/03/03 Python
HTML5文档结构标签
2017/04/21 HTML / CSS
域名注册、建站工具、网页主机、SSL证书:Dynadot
2017/01/06 全球购物
viagogo意大利票务平台:演唱会、体育比赛、戏剧门票
2018/01/26 全球购物
澳大利亚拥有最好的家具和家居用品在线目的地:Nestz
2019/02/23 全球购物
实习护士自我鉴定
2013/10/13 职场文书
十佳护士先进事迹
2014/05/08 职场文书
普通话宣传标语
2014/06/26 职场文书
"9.18"国耻日演讲稿范文
2014/09/14 职场文书
2015年九一八事变纪念活动实施方案
2015/05/06 职场文书
幼儿园教师管理制度
2015/08/05 职场文书
什么是检讨书?检讨书的格式及范文
2019/11/05 职场文书
Python爬虫入门案例之爬取去哪儿旅游景点攻略以及可视化分析
2021/10/16 Python