• python-wordcloud词云


    导入模块

    from wordcloud import WordCloud
    import jieba
    import imageio
    import matplotlib.pyplot as plt
    from PIL import ImageGrab
    import numpy as np
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    wordcloud以空格为分隔符号,来将文本分隔成单词

    PIL pillow模块

    img = imageio.imread('image.png')
    
    • 1

    这行代码使用imageio库读取一个名为“image.png”的图像文件,并将图像作为numpy数组存储在变量“img”中

    在这里插入图片描述
    dir可以查看一些东西

    WordCloud对象创建的常用参数

    • font_path:字体文件的路径 - - - 默认None
    • widthheight:词云生成图片的宽高 - - - 默认宽400px,高200px
    • mask:词云形状 - - -默认None(方形图)
    • min_font_sizemax_font_size:词云中最大最小的字体字号 - - - 最小4号 最大根据高度自动调节
    • font_step:字号步进间隔 - - - 默认1
    • max_words:最大次数 - - - 200
    • stopwords:被排除的词列表,排除词不在词云中显示 - - - stopwords={‘python’}
    • background_color:图片背景色 - - - 黑色
    • repeat=True:词太少时可以让词重复出现在词云中
    • contour_widthcontour_color:添加词云边框和边框颜色
    • colormap:修改字体颜色
      Matplotlib附带的色彩映射参考
      在这里插入图片描述

    WordCloud类的常用方法

    • generate(text):由text文本生成词云
    • to_file(filename):将词云图保存为名为filename的文件
    • to_image() :可以直接在jupyter里面看到词云的图片

    案例

    from wordcloud import WordCloud
    
    w = WordCloud()
    w.generate('hi hi hello hi hi hello world!')
    w.to_file('hi.png')
    
    • 1
    • 2
    • 3
    • 4
    • 5

    在这里插入图片描述

    import wordcloud
    
    w = wordcloud.WordCloud(background_color='white',repeat=True)
    text = 'hi,hello world!'
    
    w.generate(text) 
    w.to_image()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    在这里插入图片描述

    w = wordcloud.WordCloud(background_color='white',repeat=True,colormap='PuRd_r')
    
    • 1

    在这里插入图片描述

    mask = np.array(PIL.Image.open('aixin.png'))
    w = wordcloud.WordCloud(mask=mask,background_color='white',repeat=True,colormap='PuRd_r')
    
    • 1
    • 2

    默认mask表示为binary(二进制)
    对应参数是numpy 中的 array数组,将图片用PIL库打开 使用矩阵表示出来(图像本质就是矩阵)
    在这里插入图片描述

    mask = np.array(PIL.Image.open('aixin.png'))
    w = wordcloud.WordCloud(mask=mask,background_color='white',repeat=True,colormap='RdBu',contour_color='black',contour_width=5)
    
    • 1
    • 2

    在这里插入图片描述

    w = wordcloud.WordCloud(mode='RGBA',mask=mask,background_color='white',repeat=True,colormap='RdBu')
    
    • 1

    mode=‘RGBA’ 保存的图片不能为.jpg后缀,可以使用png

    from wordcloud import WordCloud
    import imageio
    import matplotlib.pyplot as plt
    
    mk = imageio.imread('aixin.png')  # 打开图片文件
    w = WordCloud(mask=mk,background_color='lightpink',font_path='msyh.ttc',colormap='Accent',min_font_size=2,stopwords={'就在这时'}) # msyh微软雅黑字体
    f = open('data.txt','r',encoding='utf-8')
    w.generate(f.read())
    plt.imshow(w)   # 显示词云
    plt.axis('off') # 隐藏坐标轴
    plt.show()
    w.to_file('aixincy.png') # 保存的词云图片大小和mask图片的大小一样
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    在这里插入图片描述

    w.generate(" ".join(jieba.lcut(txt)))即为用空格的方法去分隔jieba库精确模式下形成的字符串。

    jieba自带的词库包括:

    1. dict.txt.big - 大型词库,包含约2.7万个词汇和常用词语

    2. dict.txt.small - 小型词库,包含约1.4万个词汇和常用词语

    3. user.dict - 用户自定义词库,用户可以将自己的词汇添加到此文件中

    4. stop_words.txt - 停用词词典,包含约1000个常用停用词

    5. idf.txt - 关键词权重词典,用于提取文本中的关键词

    6. stop_words_cn.txt - 中文停用词词典,包含约1500个常用停用词

    7. stopwords.txt - 英文停用词词典,包含约400个常用停用词jieba自带的词库包括:

  • 相关阅读:
    【RV1103】RTL8723bs (SD卡形状模块)驱动开发
    Java---Stream流详解
    webpack5基础和配置
    关于BigInteger和BigDecimal
    内卷时代下的前端技术-使用JavaScript在浏览器中生成PDF文档
    linux安装opencv
    LM09丨费雪逆变换反转网格策略
    地图数据设计(二):矢量数据检查与错误处理
    Leetcode刷题详解——打家劫舍 II
    在GORM中使用并发
  • 原文地址:https://blog.csdn.net/weixin_64729620/article/details/132669671