【Python行业分析5】BOSS直聘招聘信息获取之热词图wordcloud

在这里插入图片描述
wordcloud 是 Python中的小词云生成器。

  • 填充所有可用空间
  • 能够使用任意背景
  • 具有高效的实现方式,可以轻松对其进行修改

wordcloud依赖于numpy和pillow。
支持Python 2.7、3.4、3.5、3.6和3.7。
pip3 install wordcloud进行安装

from wordcloud import WordCloud
import csv


def createImage(file_name):
    tags = []
    with open(file_name, "r", encoding='utf8') as f:
        file_content = csv.reader(f)
        for row in file_content:
            tags += row[13].split("、")

    wordcloud = WordCloud(
        font_path='C:/Windows/Fonts/simhei.ttf',
        width=700,
        height=400,
        max_words=200,
        max_font_size=150
    ).generate(" ".join(tags))
    image = wordcloud.to_image()
    image.show()

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

参数 说明
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),然后显示的形状为不是白色的其他颜色。
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) 将长文本分词并去除屏蔽词
recolor([random_state, color_func, colormap]) 对现有输出重新着色。重新上色会比重新生成整个词云快很多。
to_array() 转化为 numpy array
to_file(filename) 输出到文件

微信搜一搜关注博主领取更多学习咨料在这里插入图片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章