一:wordcloud介绍

wordcloud库可以说是python非常优秀的词云展示第三方库。词云以词语为基本单位更加直观和艺术的展示文本,wordcloud库是基于numpy和pillow这两个内置库的。官网地址:wordcloud,github地址:word_cloud
在这里插入图片描述

二:wordcloud安装

可以直接使用pip安装:

pip install wordcloud

在这里插入图片描述我应该算是幸运的吧,我看网上一堆人都是说第一次装绝对会出错,我这次装虽然没有报错,但也有一堆warning,感觉报错的主流解法都是直接下载whl文件安装,如果安装出错的话这里推荐一个安装教程:wordcloud安装方法(Windows10)

三:wordcloud的使用

我们可以通过wordcloud方法来生成我们的词云对象。我们在定义对象的时候也能生成我们的对象的参数定义:

  • width:图像的宽度,默认为400px
  • height:图像的高度,默认为200px
  • min_font_size:最小字号,默认为4px
  • max_font_size:最大字号,默认不做限制
  • margin:画布边缘留白的空隙,默认留白空间是2px
  • font_step:字体步进,系统会根据词出现的次数来定词的大小,次数多的和次数少的之间的字号差距就是步进间隔。默认为1
  • font_path:展示字体的路径
  • prefer_horizontal: 词语水平方向排版出现的频率,默认 0.9 ,所以词语垂直方向排版出现频率为 0.1
  • scale:在字段width和height乘以的倍数,最终呈现的画布尺寸以这个结果。默认是1,此方法适合需要呈现大尺寸的画布
  • max_words:词云最大单词数量,默认为200
  • stop_words:不显示词语列表,采用集合形式:stop_words={" “,” “,” "}
  • background_color:背景颜色,默认为黑色
  • normalize_plurals:bool类型,是否去掉单词末尾的s,默认去掉
  • repeat:bool类型,单词是否重复展示,默认不重复

除了上面这些参数,wordcloud还有一个特殊的参数,就是词云的形状,wordcloud的形状我们必须引入另外一个库imageio,通过imageio我们可以将目前的图片加载成元单元。

pip install imageio

引入完成后,我们可以设置词云的形状,参数为 mask

wordcloud的一些方法:

 方法名   参数 返回值 备注
fit_words(frequencies)frequencies:dict from string to floatself 根据单词及其频率生成词云

generate_from_frequencies

(frequenciesmax_font_size=None)

frequencies:dict from string to float

max_font_size:int

self
generate(text)text:stringself根据文本生成词云,是方法generate_from_text的别称。输入的文本应该是一个自然文本。若输入的是已排列好的单词,那么单词会出现两次,可以设置参数collocations=False去除此单词重复。调用process_text和genereate_from_frequences
generate_from_text(text)text:stringself
process_text(text)text:stringwords:dict (string, int)将一长段文本切片成单词,并去除stopwords。返回单词(words)和其出现次数的字典格式
recolor(random_state=Nonecolor_func=Nonecolormap=None)

random_state:RandomState, int, or None, default=None

color_func:function or None, default=None

colormap:string or matplotlib colormap, default=None

self 
to_array() image:nd-array size (width, height, 3)转换成numpy array
to_file(filename)filename:stringself保存图片文件

需要注意的是,使用generate加载词云文本时,其中txt的内容需要以空格来分隔单词,并且对于英文单词而言,如果单词长度为1-2,系统会自动过滤。

效果如下:

import wordcloud, imageio

# 准备字符串
str = """If El Niños were dangerous before,they are looking to become especially destructive in the near future. 
Already severe and unpredictable,recent research indicates these natural weather events are now swinging to even 
greater extremes. Since humans started burning fossil fuels on an industrial scale,coral records from the past 7,
000 years indicate that heat waves,wildfires,droughts,flooding and violent storms associated with El Niño have grown 
markedly worse. """

# 准备词云模板
mask = imageio.imread("wordcloud/template.png")
# 实例化一个wordcloud对象
wc = wordcloud.WordCloud(width=400, height=400, min_font_size=5, max_font_size=50, font_step=2, max_words=500, mask=mask)
# 加载词云文本
wc.generate(str)
# 输出图片
wc.to_file("wordcloud/word.png")

在这里插入图片描述在这里插入图片描述

四:wordcloud案例

题目:wordcloud 是优秀的词云展示第三方库,它以以词语为基本单位,更加直观和艺术地展示文本,请根据附件文件(校长 2018.txt、校长 2018 毕业讲话.txt),分别绘制词云彩,可自行设定背
景或背景图片。

import wordcloud, imageio

# 准备字符串
f1 = open("校长2018.txt", "r+", encoding="utf-8")
f2 = open("校长2018毕业讲话.txt", "r+", encoding="utf-8")
text1 = f1.read()
text2 = f2.read()
# 准备词云模板
mask = imageio.imread("wordcloud/template.png")
# 实例化一个wordcloud对象
wc = wordcloud.WordCloud(width=400, height=400, min_font_size=5, max_font_size=50, font_step=2, max_words=500, mask=mask)
# 加载词云文本
wc.generate(text1 + text2)
# 输出图片
wc.to_file("wordcloud/word.png")

print(text1+text2)
f1.close()
f2.close()

我在运行的时候发现,没有报错,但结果却是这样的:
在这里插入图片描述
经过查阅资料后找到原因,wordcloud默认字体是DroidSansMono.ttf,这个字体不支持中文格式,因此需要更换字体,找到wordcloud.py文件,打开后更改:

在这里插入图片描述
我这里是改成了微软雅黑,同时还需要把对应的字体放在wordcloud.py同目录:
在这里插入图片描述
然后就顺利完成了:
在这里插入图片描述大家如果有兴趣了解更多相关知识,可以来我的个人网站看看:eyes++的个人空间

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐