Python制作词云
来源:互联网 发布:福建软件职业技术学院 编辑:程序博客网 时间:2024/06/07 13:25
Python制作词云
需求:
看到朋友圈有人发词云照片,感觉自己也可以玩一玩,于是乎借助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# importfrom os import pathfrom PIL import Imageimport numpy as npimport matplotlib.pyplot as pltfrom wordcloud import WordCloud, STOPWORDS# current pathd = path.dirname(__file__)# Read the whole text.text = open(path.join(d, 'test.txt')).read()# read the mask imagetest_mask = np.array(Image.open(path.join(d, "test_mask.png")))stopwords = set(STOPWORDS)stopwords.add("said")# settingwc = WordCloud(background_color="black", max_words=2000, mask=test_mask, stopwords=stopwords)# generate word cloudwc.generate(text)# plot and showplt.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 filewc.to_file(path.join(d, "test.png"))
辅助文件
test_mask.png
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
效果图
其它说明:
1.文档可以是任意英文txt文件,以上的是网络上英文新闻中的一小段,仅起示例作用。
2.关于中文支持,有多种方法,主要就是分词的问题,这里不讨论了。
3.我不清楚许多人说的定制是什么意思,因为如果想要自己任意想要的形状的话,我觉得用PS做一个png图可以达到同样的效果,经测试也没有发现问题。
4.写的第一篇文章,不足之处欢迎来喷,毕竟我是要学习的。
- Python制作词云
- Python生成词云的制作
- python 制作标签云
- python wordcloud 对电影《我不是潘金莲》制作词云
- Python使用wordcloud+pillow基于给定图像制作词云
- 在python上使用wordcloud制作自己的词云
- 用wx聊天记录 制作词云(python)
- Python爬虫入门-python之jieba库制作词云图
- Python 3.6 使用wordcloud制作词云(可设背景图像)
- python运用jieba和wordcloud库简单实现对QQ聊天记录制作词云
- python制作安装包
- 软件制作:Python
- Python Tkinter制作GUI
- python 简单游戏制作
- 【python】通讯录制作
- python的egg制作
- python制作报表
- Python制作统计图形
- 幸运数
- 如何给Android工程的libs目录下的jar包附加源码
- Verilog语言中产生随机数的方法
- 先码后看 红黑树 侵立删
- Linux学习笔记(一):Linux中直接以rwx更改文件权限
- Python制作词云
- attribute和property的区别是什么
- Linux 环境下iSCSI Target 与 Initiator 配置
- Script error .可能的原因
- ubuntu下安装mysql方法
- 网站重构
- 新闻速递|百名社长总编近距离触摸广东发展
- 五:亲自尝试压缩数据
- 编辑器工具--NGUI--图集相关--指定图集,对选中预设中所有使用本图集的图片绑定本图集