Python 制作词云的WordCloud参数用法说明
场景
官方API:
https://amueller.github.io/word_cloud/generated/wordcloud.WordCloud.html
实现
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-arrayorNone(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:setofstringsorNone#设置需要屏蔽的词,如果为空,则使用内置的STOPWORDS
background_color:colorvalue(default=”black”)#背景颜色,如background_color='white',背景颜色为白色
max_font_size:intorNone(default=None)#显示的最大的字体大小
mode:string(default=”RGB”)#当参数为“RGBA”并且background_color不为空时,背景为透明
relative_scaling:float(default=.5)#词频和字体大小的关联性
color_func:callable,default=None#生成新颜色的函数,如果为空,则使用self.color_func
regexp:stringorNone(optional)#使用正则表达式分隔输入的文本
collocations:bool,default=True#是否包括两个词的搭配
colormap:stringormatplotlibcolormap,default=”viridis”#给每个单词随机分配颜色,若指定color_func,则忽略该方法
random_state:intorNone#为每个单词返回一个PIL颜色
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()#转化为numpyarray
to_file(filename)#输出到文件
补充:生成词云之python中WordCloud包的用法
效果图:
这是python中使用wordcloud包生成的词云图。
下面来介绍一下wordcloud包的基本用法
classwordcloud.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-arrayorNone(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:setofstringsorNone//设置需要屏蔽的词,如果为空,则使用内置的STOPWORDS
background_color:colorvalue(default=”black”)//背景颜色,如background_color='white',背景颜色为白色。
max_font_size:intorNone(default=None)//显示的最大的字体大小
mode:string(default=”RGB”)//当参数为“RGBA”并且background_color不为空时,背景为透明。
relative_scaling:float(default=.5)//词频和字体大小的关联性
color_func:callable,default=None//生成新颜色的函数,如果为空,则使用self.color_func
regexp:stringorNone(optional)//使用正则表达式分隔输入的文本
collocations:bool,default=True//是否包括两个词的搭配
colormap:stringormatplotlibcolormap,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()//转化为numpyarray
to_file(filename)//输出到文件
例子:
想要生成的词云的形状:
图中黑色部分就是词云的将要显示的部分,白色部分不显示任何词。
下面是一个文本文档:
HowtheWordCloudGeneratorWorks
ThelayoutalgorithmforpositioningwordswithoutoverlapisavailableonGitHubunderanopensourcelicenseasd3-cloud.Notethatthisistheonlythelayoutalgorithmandanycodeforconvertingtextintowordsandrenderingthefinaloutputrequiresadditionaldevelopment.
Aswordplacementcanbequiteslowformorethanafewhundredwords,thelayoutalgorithmcanberunasynchronously,withaconfigurabletimestepsize.Thismakesitpossibletoanimatewordsastheyareplacedwithoutstuttering.Itisrecommendedtoalwaysuseatimestepevenwithoutanimationsasitpreventsthebrowser'seventloopfromblockingwhileplacingthewords.
Thelayoutalgorithmitselfisincrediblysimple.Foreachword,startingwiththemost“important”:
Attempttoplacethewordatsomestartingpoint:usuallynearthemiddle,orsomewhereonacentralhorizontalline.Ifthewordintersectswithanypreviouslyplacedwords,moveitonestepalonganincreasingspiral.Repeatuntilnointersectionsarefound.Thehardpartismakingitperformefficiently!AccordingtoJonathanFeinberg,Wordleusesacombinationofhierarchicalboundingboxesandquadtreestoachievereasonablespeeds.
GlyphsinJavaScript
Thereisn'tawaytoretrievepreciseglyphshapesviatheDOM,exceptperhapsforSVGfonts.Instead,wedraweachwordtoahiddencanvaselement,andretrievethepixeldata.
Retrievingthepixeldataseparatelyforeachwordisexpensive,sowedrawasmanywordsaspossibleandthenretrievetheirpixelsinabatchoperation.
SpritesandMasks
Myinitialimplementationperformedcollisiondetectionusingspritemasks.Onceawordisplaced,itdoesn'tmove,sowecancopyittotheappropriatepositioninalargerspriterepresentingthewholeplacementarea.
Theadvantageofthisisthatcollisiondetectiononlyinvolvescomparingacandidatespritewiththerelevantareaofthislargersprite,ratherthancomparingwitheachpreviouswordseparately.
Somewhatsurprisingly,asimplelow-levelhackmadeatremendousdifference:whenconstructingthespriteIcompressedblocksof321-bitpixelsinto32-bitintegers,thusreducingthenumberofchecks(andmemory)by32times.
Infact,thisturnedouttobeatmyhierarchicalboundingboxwithquadtreeimplementationoneverythingItriediton(evenverylargeareasandfontsizes).Ithinkthisisprimarilybecausethespriteversiononlyneedstoperformasinglecollisiontestpercandidatearea,whereastheboundingboxversionhastocomparewitheveryotherpreviouslyplacedwordthatoverlapsslightlywiththecandidatearea.
Anotherpossibilitywouldbetomergeaword'streewithasinglelargetreeonceitisplaced.Ithinkthisoperationwouldbefairlyexpensivethoughcomparedwiththeanalagousspritemaskoperation,whichisessentiallyORingawholeblock.
从这个文本中生成一个词云,代码如下:
#!/usr/bin/python
#-*-coding:utf-8-*-
#coding=utf-8
#导入wordcloud模块和matplotlib模块
fromwordcloudimportWordCloud
importmatplotlib.pyplotasplt
fromscipy.miscimportimread
#读取一个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')
运行结果:
以上为个人经验,希望能给大家一个参考,也希望大家多多支持毛票票。如有错误或未考虑完全的地方,望不吝赐教。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。