zoukankan      html  css  js  c++  java
  • WordCloud 简介

    WordCloud 简介

    GitHub 

    GitHub:https://github.com/amueller/word_cloud

    example:https://github.com/amueller/word_cloud/tree/master/examples

    wordcloud 是什么?

    词云图

    说wordcloud 之前我们要先了解一个名词,词云图 ,什么是词云图呢?

    词云图,也叫文字云,是对文本中出现频率较高的“关键词”予以视觉化的展现,词云图过滤掉大量的低频低质的文本信息,使得浏览者只要一眼扫过文本就可领略文本的主旨。

     

    WordCloud

    WordCloud 是一款python环境下的词云图工具包,同时支持python2和python3,能通过代码的形式把关键词数据转换成直观且有趣的图文模式。

    安装

    pip默认安装方式

    pip install wordcloud

    如果是使用conda的方式安装,则使用以下命令安装

    conda install -c conda-forge wordcloud

    wordcloud输入第三方安装包,也可以下载whl文件到本地然后本地安装

    1、先检查自己使用的python是哪个版本的

    python --version
    
    Python 3.7.1

    2、打开非正式第三方whl文件包的网站  https://www.lfd.uci.edu/~gohlke/pythonlibs/   ,下载对应版本的安装包,我这里是python3.7的windows版本,所以选择最后一个

    image.png

    3、下载完成以后打开cmd,用pip安装wheel 执行命令

    pip install wheel

    4、cmd切换到刚才下载的 wordcloud-1.5.0-cp37-cp37m-win_amd64.whl   目录,然后执行命令

    pip install wordcloud-1.5.0-cp37-cp37m-win_amd64.whl
    

      

    如图,证明安装成功

    使用方法

    简单实例讲解:

       with open("tmp/tag.txt", encoding="utf-8") as file:
                # 数据文件
                txt = file.read()
                # 如果数据文件中包含的有中文的话,font_path必须指定字体,否则中文会乱码
                # collocations:是否包括两个词的搭配,默认为True,如果为true的时候会有
                # 重复的数据,这里我不需要重复数据,所以设置为False
                # width 幕布的宽度,height 幕布的高度
                # max_words 要显示的词的最大个数
                # generate 读取文本文件
                wordcloud = WordCloud(font_path="C:/Windows/Fonts/simfang.ttf", 
                											collocations=False,
                                      background_color="black", 
                                      width=800, 
                                      height=600, 
                                      max_words=100).generate(txt)
                # 生成图片
                image = wordcloud.to_image()
                # 展示图片
                image.show()
                # 写入文件
                wordcloud.to_file("tmp/tag.jpg")
    

      

    更多官方示例讲解

     

    https://github.com/amueller/word_cloud/blob/master/examples/a_new_hope.py

    参数讲解

    详细参数讲解请查看  https://www.yuque.com/darren-irbls/python/pr2zc5

    属性

    数据类型|默认值

    解析

    font_path

    string

    字体路径
    windows:C:/Windows/Fonts/
    Linux: /usr/share/fonts
    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 取代
     
    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”
    背景颜色
    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,则忽略该方法
    random_state 
    int or None
    为每个单词返回一个PIL颜色

    在线词云图工具

    wordart

    https://wordart.com/

    图悦

    http://www.picdata.cn/indexb.php

    worditout

    https://worditout.com/word-cloud/create

  • 相关阅读:
    怎样看文献
    How to save rules of the iptables?
    Keras 自适应Learning Rate (LearningRateScheduler)
    在主线程中慎用WaitForSingleObject (WaitForMultipleObjects)
    QT5.9 新特性与版本回顾
    [常见问题]解决创建servlet 找不到webservlet包.
    MyBatis学习总结(八)——Mybatis3.x与Spring4.x整合
    MyBatis学习总结(七)——Mybatis缓存
    MyBatis学习总结(六)——调用存储过程
    MyBatis学习总结(五)——实现关联表查询
  • 原文地址:https://www.cnblogs.com/jesn/p/10312706.html
Copyright © 2011-2022 走看看