生成词云之python中WordCloud包的用法

效果图:
词云图

这是python中使用wordcloud包生成的词云图。

下面来介绍一下wordcloud包的基本用法。

class wordcloud.WordCloud(font_path=None, width=400, height=200, margin=2, ranks_only=None, prefer_horizontal=0.9,mask=None, scale=1, color_func=None, max_words=200, min_font_size=4, stopwords=None, random_state=None,background_color='black', max_font_size=None, font_step=1, mode='RGB', relative_scaling=0.5, regexp=None, collocations=True,colormap=None, normalize_plurals=True)

这是wordcloud的所有参数,下面具体介绍一下各个参数:

font_path : string //字体路径,需要展现什么字体就把该字体路径+后缀名写上,如:font_path = '黑体.ttf'

width : int (default=400) //输出的画布宽度,默认为400像素

height : int (default=200) //输出的画布高度,默认为200像素

prefer_horizontal : float (default=0.90) //词语水平方向排版出现的频率,默认 0.9 (所以词语垂直方向排版出现频率为 0.1 )

mask : nd-array or None (default=None) //如果参数为空,则使用二维遮罩绘制词云。如果 mask 非空,设置的宽高值将被忽略,遮罩形状被 mask 取代。除全白(#FFFFFF)的部分将不会绘制,其余部分会用于绘制词云。如:bg_pic = imread('读取一张图片.png'),背景图片的画布一定要设置为白色(#FFFFFF),然后显示的形状为不是白色的其他颜色。可以用ps工具将自己要显示的形状复制到一个纯白色的画布上再保存,就ok了。

scale : float (default=1) //按照比例进行放大画布,如设置为1.5,则长和宽都是原来画布的1.5倍。

min_font_size : int (default=4) //显示的最小的字体大小

font_step : int (default=1) //字体步长,如果步长大于1,会加快运算但是可能导致结果出现较大的误差。

max_words : number (default=200) //要显示的词的最大个数

stopwords : set of strings or None //设置需要屏蔽的词,如果为空,则使用内置的STOPWORDS

background_color : color value (default=”black”) //背景颜色,如background_color='white',背景颜色为白色。

max_font_size : int or None (default=None) //显示的最大的字体大小

mode : string (default=”RGB”) //当参数为“RGBA”并且background_color不为空时,背景为透明。

relative_scaling : float (default=.5) //词频和字体大小的关联性

color_func : callable, default=None //生成新颜色的函数,如果为空,则使用 self.color_func

regexp : string or None (optional) //使用正则表达式分隔输入的文本

collocations : bool, default=True //是否包括两个词的搭配

colormap : string or matplotlib colormap, default=”viridis” //给每个单词随机分配颜色,若指定color_func,则忽略该方法。



fit_words(frequencies)  //根据词频生成词云
generate(text)  //根据文本生成词云
generate_from_frequencies(frequencies[, ...])   //根据词频生成词云
generate_from_text(text)    //根据文本生成词云
process_text(text)  //将长文本分词并去除屏蔽词(此处指英语,中文分词还是需要自己用别的库先行实现,使用上面的 fit_words(frequencies) )
recolor([random_state, color_func, colormap])   //对现有输出重新着色。重新上色会比重新生成整个词云快很多。
to_array()  //转化为 numpy array
to_file(filename)   //输出到文件

例子:
想要生成的词云的形状:
词云形状
图中黑色部分就是词云的将要显示的部分,白色部分不显示任何词。

下面是一个文本文档:

How the Word Cloud Generator Works

The layout algorithm for positioning words without overlap is available on GitHub under an open source license as d3-cloud. Note that this is the only the layout algorithm and any code for converting text into words and rendering the final output requires additional development.

As word placement can be quite slow for more than a few hundred words, the layout algorithm can be run asynchronously, with a configurable time step size. This makes it possible to animate words as they are placed without stuttering. It is recommended to always use a time step even without animations as it prevents the browser’s event loop from blocking while placing the words.

The layout algorithm itself is incredibly simple. For each word, starting with the most “important”:

Attempt to place the word at some starting point: usually near the middle, or somewhere on a central horizontal line.
If the word intersects with any previously placed words, move it one step along an increasing spiral. Repeat until no intersections are found.
The hard part is making it perform efficiently! According to Jonathan Feinberg, Wordle uses a combination of hierarchical bounding boxes and quadtrees to achieve reasonable speeds.

Glyphs in JavaScript

There isn’t a way to retrieve precise glyph shapes via the DOM, except perhaps for SVG fonts. Instead, we draw each word to a hidden canvas element, and retrieve the pixel data.

Retrieving the pixel data separately for each word is expensive, so we draw as many words as possible and then retrieve their pixels in a batch operation.

Sprites and Masks

My initial implementation performed collision detection using sprite masks. Once a word is placed, it doesn't move, so we can copy it to the appropriate position in a larger sprite representing the whole placement area.

The advantage of this is that collision detection only involves comparing a candidate sprite with the relevant area of this larger sprite, rather than comparing with each previous word separately.

Somewhat surprisingly, a simple low-level hack made a tremendous difference: when constructing the sprite I compressed blocks of 32 1-bit pixels into 32-bit integers, thus reducing the number of checks (and memory) by 32 times.

In fact, this turned out to beat my hierarchical bounding box with quadtree implementation on everything I tried it on (even very large areas and font sizes). I think this is primarily because the sprite version only needs to perform a single collision test per candidate area, whereas the bounding box version has to compare with every other previously placed word that overlaps slightly with the candidate area.

Another possibility would be to merge a word’s tree with a single large tree once it is placed. I think this operation would be fairly expensive though compared with the analagous sprite mask operation, which is essentially ORing a whole block. 

从这个文本中生成一个词云,代码如下:

#!/usr/bin/python
# -*- coding: utf-8 -*-
#coding=utf-8

#导入wordcloud模块和matplotlib模块
from wordcloud import WordCloud
import matplotlib.pyplot as plt
from scipy.misc import imread


#读取一个txt文件

text = open('test.txt','r').read()

#读入背景图片

bg_pic = imread('3.png')

#生成词云

wordcloud = WordCloud(mask=bg_pic,background_color='white',scale=1.5).generate(text)

image_colors = ImageColorGenerator(bg_pic)
#显示词云图片

plt.imshow(wordcloud)
plt.axis('off')
plt.show()


#保存图片

wordcloud.to_file('test.jpg')

运行结果:
最终生成的词云图

  • 46
    点赞
  • 128
    收藏
    觉得还不错? 一键收藏
  • 10
    评论
### 回答1: 您可以使用 python 的第三方库 "wordcloud" 来生成词云。首先需要安装该库,您可以使用以下命令进行安装: ``` pip install wordcloud ``` 然后,您可以通过以下代码生成词云: ```python from wordcloud import WordCloud import matplotlib.pyplot as plt text = "词云生成的文本" wordcloud = WordCloud().generate(text) plt.imshow(wordcloud, interpolation='bilinear') plt.axis("off") plt.show() ``` 您也可以调整生成词云的其他参数,例如字体,背景颜色等。更多内容请参考官方文档:https://amueller.github.io/word_cloud/ ### 回答2: Python123wordcloud是一个用于生成词云Python库。 通过使用Python123wordcloud,我们可以轻松地根据给定的文本数据生成词云图词云图是一种可视化工具,用于显示文本不同词语的频率和重要性。生成词云图可以帮助我们更直观地理解文本的关键词信息。 使用Python123wordcloud的基本步骤如下: 1. 导入Python123wordcloud库:在Python脚本使用`import wordcloud`语句导入Python123wordcloud库。 2. 准备文本数据:将需要生成词云图的文本数据准备好,并存储在一个变量。 3. 创建WordCloud对象:使用`wc = wordcloud.WordCloud()`创建一个WordCloud对象。可以通过设置不同的参数来自定义词云图的样式,如词云图的形状、字体、颜色等。 4. 生成词云图:使用`wc.generate(text)`方法生成词云图,其`text`是之前准备好的文本数据。 5. 显示词云图:使用`import matplotlib.pyplot as plt`导入matplotlib库,并使用`plt.imshow(wc)`和`plt.axis("off")`来显示词云图,其`wc`是之前生成词云对象。最后使用`plt.show()`来展示词云图Python123wordcloud还提供了其他的一些功能,如根据颜色、字体大小等参数对词云图进行定制化调整,或者通过设置屏蔽词来排除一些无关的词语。 总而言之,使用Python123wordcloud可以方便地生成词云图,帮助我们更好地理解文本数据的关键词信息。 ### 回答3: Python123WordCloud是一个用Python编程语言编写的词云生成工具。通过该工具,我们可以根据给定的文本数据生成美观、有趣的词云图像。 使用Python123WordCloud生成词云的步骤如下: 1. 导入相关库:首先需要导入必要的库,如wordcloud、matplotlib和numpy。 2. 加载文本数据:将需要生成词云的文本数据加载到程序。可以是从文件读取的文本,或者直接将文本赋值给一个字符串变量。 3. 对文本数据进行处理:对文本进行必要的清洗和处理。可以使用正则表达式、nltk等工具进行去除噪音、分词、去除停用词等操作,以便获取更准确的词云结果。 4. 创建词云对象:创建一个WordCloud对象,可以根据需要设置词云的参数,如字体、颜色、背景颜色、尺寸等。 5. 生成词云图像:调用WordCloud对象的generate方法,传入处理好的文本数据,生成词云图像。 6. 显示或保存词云图像:使用matplotlib库将生成词云图像显示出来,也可以保存为文件。 Python123WordCloud提供了许多参数可以自定义词云图像的样式和风格,如设置最大词汇数、设置词云形状等。通过调整这些参数,我们可以根据实际需求生成满足我们需要的词云图像。 总之,Python123WordCloud是一个方便易用的词云生成工具,可以通过简单的几步操作生成具有艺术感与观赏性的词云图像,可用于数据分析、文本可视化等领域。
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值