wordcloud

安装词云
pip install 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)
参数类型描述
font_pathstring指定字体的位置(可以在网上下载OTF或者TTF格式),默认使用DroidSansMono path
widthint(default=400)图片宽度,越大越精细
heightint (default=200)图片高度,越大越精细
prefer_horizontalfloat (default=0.90)越接近1说明越喜欢水平的字,则字就不会为了适合展示而翻转成水平的
mask : nd-array or None (default=None)将词云画在遮罩上
scale : float (default=1)计算和画图的比例,当做大图的时候使用scale代替height和width速度会快,但会影响词之间的拟合度
min_font_size : int (default=4)最小频率词的大小
font_step : int (default=1)字体步长,如果大于1,可以增快计算速度,但是拟合度会下降
max_words : number (default=200)图片容纳词语的上限
stopwords : set of strings or None设置停用词,如果不设置则使用默认的停用词
background_color : color value (default=”black”)背景颜色设置
max_font_size : int or None (default=None)最大的词语的大小
mode : string (default=”RGB”)当使用”RGBA”时,背景将变成透明的
relative_scaling : float (default=.5)当relative_scaling=0时,词的大小是按照排名计算的,当relative_scaling=1时候则会按词频的倍数计算大小。
color_func : callable, default=None根据每个词的font_size, position, orientation, font_path, random_state等属性为词语生成特定的颜色,具体参考matplotlib colormap
regexp : string or None (optional)
collocations : bool, default=True是否考虑词语的搭配
colormap : string or matplotlib colormap, default=”viridis”Matplotlib colormap,如果color_func参数被指定,则此参数无效
normalize_plurals : bool, default=True是否忽略复数
属性类型描述
words_(dict of string to float)返回词频
layout_(list of tuples (string, int, (int, int), int, color)))词云词的属性信息
方法描述
fit_words(frequencies)元组形式(word,freq)
generate(text)文本形式
generate_from_frequencies(frequencies[, …])字典形式{word:freq}
generate_from_text(text)
process_text(text)分割文本,去除停用词
recolor([random_state, color_func, colormap])重新对图层上色
to_array()
to_file(filename)
to_html()
to_image()

例子

from wordcloud import WordCloud
font = r'C:\Windows\Fonts\simfang.ttf'  # 指定字体
wc = WordCloud(font_path=font)
wc.generate_from_frequencies(coun)
wc.to_file("./word.png")

参考文献:
http://amueller.github.io/word_cloud/generated/wordcloud.WordCloud.html#wordcloud.WordCloud

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值