Word Cloud (词云) - Python
##### >>What's Word Cloud
词云 (Word Cloud)是对文本中出现频率较高的词语给予视觉化展示的图形, 是一种常见的文本挖掘的方法。目前已有多种数据分析工具支持这种图形,如Matlab, SPSS, SAS, R 和 Python 等等,也有很多在线网页能生成 word cloud, 例如wordclouds.com
##### >> Create Word Cloud via Python
Python 可以使用 wordcloud 模块来生成词云。
安装 wordcloud, matplotlib 及其依赖模块。
准备文本。
我从维基百科中找到一段关于 Word Cloud History 的文字,以下将以这段文字为例。复制这段文字到 NotePad,并将其保存为 .*txt 文本格式。
- 运行 Python script。
"""
Python Example
===============
Generating a wordcloud from the txt file using Python.
"""
from wordcloud import WordCloud
# Read the whole text from txt.
fp = "C:/Users/yuki/Desktop/WordCloudHistory.txt"
text = open(fp).read()
# Generate a word cloud image
wordcloud = WordCloud(
font_path = "C:/Windows/Fonts/BROADW.TTF",
width = 600, #width of the canvas.
height = 400, #height of the canvas.
max_font_size = 60,
font_step = 1,
background_color = "white",
random_state = 1,
margin = 2,
colormap = "tab20" #matplotlib colormap
).generate(text)
# Display the generated image in matplotlib way:
import matplotlib.pyplot as plt
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis("off")
plt.show()
- 生成 word cloud。
##### >> Notes
在使用 wordcloud 模块的时候曾发现某些词语的频率(或者权重)是一样的,但是在生成的图形中字体大小却不一样。
Google 后找到开发作者的回答:
The algorithm might give more weight to the ranking of the words than their actual frequencies, depending on the max_font_size and the scaling heuristic.
The scaling is relative to the size of the figure and the frequency of the words. The frequencies are normalized against the max frequency, so the absolute values are irrelevant.
大概是为了将词语尽可能地填满画布,wordcloud 算法会自动根据 max_font_size 和 scale 自动调整词语的权重。那么 wordcloud 生成的图形词语大小和他的词频(或者权重)的绝对值并不是一一对应的关系。
我觉得嘛:虽然这样画出的图形比较好看,但还是觉得有点奇怪,毕竟按词频大小展示词语应该是 word cloud 这种图形的精髓。
##### >> Sample Code
>> Related Blogs
1. [Word Cloud (词云) - JavaScript](https://www.cnblogs.com/yukiwu/p/10968816.html)
2. [Word Cloud (词云) - R](https://www.cnblogs.com/yukiwu/p/10969250.html)
3. [Word Cloud (词云) - Matlab](https://www.cnblogs.com/yukiwu/p/10971998.html)
Word Cloud (词云) - Python的更多相关文章
- Word Cloud (词云) - Matlab
今天要总结的是 Word Cloud 最后一个部分了,用 Matlab 来创建 word cloud.Matlab R2018b 已经提供 wordcloud 函数可以直接生成词云了. >> ...
- Word Cloud (词云) - R
在前面已经陆续总结了如何用 Python 和 JavaScript 创建词云了,今天要说的是 R.其实 SPSS 和 SAS 的 Word Cloud 扩展模板都是基于 R 实现的. >> ...
- Word Cloud (词云) - JavaScript
在上一篇中已经分享了用 Python 创建词云了.接下来继续总结其他创建词云的方法. >> Create Word Cloud via JavaScript JavaScript 可以借助 ...
- 从CentOS安装完成到生成词云python学习日记
欢迎访问我的个人博客:原文链接 前言 人生苦短,我用python.学习python怎么能不搞一下词云呢是不是(ง •̀_•́)ง 于是便有了这篇边实践边记录的笔记. 环境:VMware 12pro + ...
- 根据词频生成词云(Python wordcloud实现)
网上大多数词云的代码都是基于原始文本生成,这里写一个根据词频生成词云的小例子,都是基于现成的函数. 另外有个在线制作词云的网站也很不错,推荐使用:WordArt 安装词云与画图包 pip3 insta ...
- python 生成18年写过的博客词云
文章链接:https://mp.weixin.qq.com/s/NmJjTEADV6zKdT--2DXq9Q 回看18年,最有成就的就是有了自己的 博客网站,坚持记录,写文章,累计写了36篇了,从一开 ...
- 爬虫之绘图matplotlib与词云(七)
1 绘制条形图 import matplotlib # 数据可视化 from matplotlib import pyplot as plt # 配置字体 matplotlib.rcParams[&q ...
- Matplotlib学习---用wordcloud画词云(Word Cloud)
画词云首先需要安装wordcloud(生成词云)和jieba(中文分词). 先来说说wordcloud的安装吧,真是一波三折.首先用pip install wordcloud出现错误,说需要安装Vis ...
- 推荐一个免费的生成词云(word cloud)的在线工具
"词云"这个概念由美国西北大学新闻学副教授.新媒体专业主任里奇·戈登(Rich Gordon)提出. "词云"就是对网络文本中出现频率较高的"关键词& ...
随机推荐
- OpenWrt 安装python-sqlite3失败
https://dev.openwrt.org/ticket/12239 #12239 reopened defect Sqlite3 missing in python 汇报人: dgspai@- ...
- 高端技巧:怎样使用#define定义变量
Introduction 想在源文件里定义一个跟行号有关的变量,每次都手动输入实在是太慢了.本文介绍怎样使用宏定义来定义与行号有关的变量. 比如:我们想在源码的第10行定义A_10这种一个整形变量. ...
- form怎样正确post文件
form在HTML中,是用于收集用户输入的,基本全部浏览器都支持form.给form加入method属性.就能实现将用户在form内控件输入的信息POST到制定地址.或发送GET请求. 写了以下一段代 ...
- Python中的shelve模块
shelve中有用的函数就是open(),但是下面编写的数据库函数中调用路径是经常出错,如果直接调用一个从来没有用过的文件却能正常运行,暂时没有找出原因. 调用shelve.open()会返回一个sh ...
- SLF4J: Failed to load class的问题及解决
今天在做接口测试,一运行测试程序,就跳出这样一个大大的错误: SLF4J: Failed to load class “org.slf4j.impl.StaticLoggerBinder”. SLF4 ...
- jquery验证后ajax提交,返回消息怎样统一显示的问题
/* jquery验证后ajax提交.返回消息怎样跟jquery验证体系统一显示的问题,网上查了非常多资料.都没有找到明白的答案,通过数小时的尝试,最终攻克了,现举一个简单的样例,给须要的人參考參考吧 ...
- Openstack-Ceilometer-获取主机内存 的使用
1. 物理server配置 1.1安装 參考 http://blog.csdn.net/qq_21398167/article/details/47019751 1.2 配置 关闭selin ...
- 【bzoj2462】[BeiJing2011]矩阵模板
#include<algorithm> #include<iostream> #include<cstdlib> #include<cstring> # ...
- Koa2学习(七)使用cookie
Koa2学习(七)使用cookie Koa2 的 ctx 上下文对象直接提供了cookie的操作方法set和get ctx.cookies.set(name, value, [options])在上下 ...
- pymssql.connect(server='.', user='', password='', database='', timeout=0, login_timeout=60, charset='UTF-8', as_dict=False, host='', appname=None, port='1433', conn_properties, autocommit=False, tds_
http://pymssql.org/en/stable/ref/pymssql.html """ This is an effort to convert the py ...