基于python生成词云图的代码示例

 更新时间:2023年11月16日 08:32:07   作者:颜酱  
这篇文章主要个介绍了如何基于python生成词云图的代码示例,文中有详细的代码示例喝图文讲解,对大家的学习或工作有一定的帮助,需要的朋友可以参考下

类似生成各种形状的云图,我也会啦!

其实也就是针对新手吧,我估计老手分分钟,哈哈哈,我怕我忘了,随手记录下~

四个步骤吧:

  • 读文件
  • 分词
  • 生成词云图
  • 显示词云图

1. 读文件

使用了下 codecs 库,读取内容更方便。

import codecs
def get_file_content(filePath):
  with codecs.open(filePath, 'r', 'utf-8') as f:
      txt = f.read()
  return txt

2. 分词

jieba是一个分词库,可以将一段文本分割成词语。cut 是将文本精确切分开,不存在冗余词语。比如颜酱是一个厉害的厨师,会变成['颜酱', '厉害', '厨师']Counter是一个计数器,可以统计词语出现的次数,most_common 是取出最常用的词语。

import jieba
from collections import Counter
def get_words(txt):
    # 先分词,得到词语数组
    seg_list = jieba.cut(txt)
    # 开始计数
    c = Counter()
    for x in seg_list:
        if len(x)>1 and x != '\r\n':
            c[x] += 1
    word_list = []
    print('常用词频度统计结果')
     # 统计前99个词
    for (k,v) in c.most_common(99):
        word_list.append(str(k))
    # 将词语生成文本文件
    file = open("./dist/out_words.txt", 'w').close()
    with open("./dist/out_words.txt",'a+',encoding='utf-8') as writeFile:
        for (k,v) in c.most_common(99):    # 统计前99个词
            writeFile.write(str(k))
            writeFile.write(str(v))
            writeFile.write('\n')
    print(word_list)
    # ['发展','平安']
    return word_list

3. 生成云图

wordcloud 是一个词云库,可以将词语生成词云图片。

import wordcloud;
def generate_cloud_image(file_path, shape_image_path):
  word_list = get_words(get_file_content(file_path))
  string = ' '.join(word_list)
  # 读取词云形状图片
  image = imageio.v2.imread(shape_image_path)
  # 先实例化一个词云对象
  wc = wordcloud.WordCloud(width=image.shape[0],     # 词云图宽度同原图片宽度
                          height=image.shape[1],
                          background_color='white',  # 背景颜色白色
                          font_path='Arial Unicode.ttf',    # 指定字体路径,微软雅黑,可从自带的字体库中找
                          mask=image,   # mask 指定词云形状图片,默认为矩形
                          scale=3)      # 默认为1,越大越清晰
  # 生成词云
  wc.generate(string)
  # 保存成文件,output_wordcloud.png,词云图
  wc.to_file('dist/output_wordcloud.png')
  # 弹出图片显示
  alert_image('dist/output_wordcloud.png')

4. 显示词云图

matplotlib是一个绘图库,可以将图片显示出来,plt 用于显示图片,mpimg 用于读取图片。

#
# plt用于显示图片
import matplotlib.pyplot as plt
# mpimg 用于读取图片
import matplotlib.image as mpimg
def alert_image(image_path):
  # 这里是让词云图弹出来显示
  lena = mpimg.imread(image_path) # 读取和代码处于同一目录下的 lena.png
  # 此时 lena 就已经是一个 np.array 了,可以对它进行任意处理
  lena.shape #(512, 512, 3)
  plt.imshow(lena) # 显示图片
  plt.axis('off') # 不显示坐标轴
  plt.show()

这就可以了!

其他文件

准备好文件实验:

input.txt

input.txt

cloud.jpg

generate_cloud_image.py

generate_cloud_image.py:

import codecs
import jieba
import imageio
import wordcloud
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from collections import Counter
# def get_words(txt):...
# def get_file_content:...
# def alert_image(image_path):...
# def generate_cloud_image(file_path, shape_image_path):...
generate_cloud_image('input.txt', 'cloud.jpg')

完整版:

import codecs
import jieba
import imageio
import wordcloud
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from collections import Counter
#  get_words函数用于统计词频,生成out.txt,展示词语和词频 如发展218 坚持170
def get_words(txt):
    seg_list = jieba.cut(txt)
    c = Counter()
    for x in seg_list:
        if len(x)>1 and x != '\r\n':
            c[x] += 1
    word_list = []
    print('常用词频度统计结果')
     # 统计前99个词
    for (k,v) in c.most_common(99):
        word_list.append(str(k))

    file = open("./out_words.txt", 'w').close()
    with open("./out_words.txt",'a+',encoding='utf-8') as writeFile:
        for (k,v) in c.most_common(99):    # 统计前99个词
            writeFile.write(str(k))
            writeFile.write(str(v))
            writeFile.write('\n')
    print(word_list)
    # ['发展','平安']
    return word_list

def get_file_content(filePath):
    with codecs.open(filePath, 'r', 'utf-8') as f:
        txt = f.read()
    return txt
def alert_image(image_path):
    # 这里是让词云图弹出来显示
    lena = mpimg.imread(image_path) # 读取和代码处于同一目录下的 lena.png
    # 此时 lena 就已经是一个 np.array 了,可以对它进行任意处理
    lena.shape #(512, 512, 3)
    plt.imshow(lena) # 显示图片
    plt.axis('off') # 不显示坐标轴
    plt.show()

# 根据文件生成词云图,file_path是文本文件路径,shape_image_path是词云图的图片途径
def generate_cloud_image(file_path, shape_image_path):
    word_list = get_words(get_file_content(file_path))
    string = ' '.join(word_list)
    # 读取词云形状图片
    image = imageio.v2.imread(shape_image_path)
    # 生成词云图片,先实例化一个词云对象
    wc = wordcloud.WordCloud(width=image.shape[0],     # 词云图宽度同原图片宽度
                            height=image.shape[1],
                            background_color='white',  # 背景颜色白色
                            font_path='Arial Unicode.ttf',    # 指定字体路径,微软雅黑,可从win自带的字体库中找
                            mask=image,   # mask 指定词云形状图片,默认为矩形
                            scale=3)      # 默认为1,越大越清晰
    # 再给词云
    wc.generate(string)
    # 保存成文件,output_wordcloud.png,词云图
    wc.to_file('output_wordcloud.png')
    alert_image('output_wordcloud.png')

generate_cloud_image('input.txt', 'cloud.jpg')

运行

记得先pip3 install jieba imageio wordcloud matplotlib然后python3 generate_cloud_image.py📢:文件在同一目录,进到这个目录下运行命令

以上就是基于python生成词云图的代码示例的详细内容,更多关于python生成词云图的资料请关注脚本之家其它相关文章!

相关文章

  • python和mysql交互操作实例详解【基于pymysql库】

    python和mysql交互操作实例详解【基于pymysql库】

    这篇文章主要介绍了python和mysql交互操作,结合实例形式详细分析了Python基于pymysql库实现mysql数据库的连接、增删改查等各种常见操作技巧,需要的朋友可以参考下
    2019-06-06
  • Python asyncio的一个坑

    Python asyncio的一个坑

    这篇文章主要介绍了Python asyncio的一个坑,文章从Python编程错误开始介绍,改变与好多变不成中常犯的错误,我们今天就来分析分析吧,需要的下伙伴也可以参考一下
    2021-12-12
  • 如何利用pyecharts画好看的饼状图

    如何利用pyecharts画好看的饼状图

    这篇文章主要给大家介绍了关于如何利用pyecharts画好看的饼状图的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-03-03
  • Python爬虫如何破解JS加密的Cookie

    Python爬虫如何破解JS加密的Cookie

    这篇文章主要介绍了Python爬虫如何破解JS加密的Cookie,帮助大家更好的理解和使用爬虫,感兴趣的朋友可以了解下
    2020-11-11
  • Python如何快速实现分布式任务

    Python如何快速实现分布式任务

    这篇文章主要介绍了Python如何快速实现分布式任务,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-07-07
  • python实现泊松图像融合

    python实现泊松图像融合

    这篇文章主要为大家详细介绍了python实现泊松图像融合,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-07-07
  • Jupyter notebook无法导入第三方模块的解决方式

    Jupyter notebook无法导入第三方模块的解决方式

    这篇文章主要介绍了Jupyter notebook无法导入第三方模块的解决方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-04-04
  • pycharm 2020 1.1的安装流程

    pycharm 2020 1.1的安装流程

    这篇文章主要介绍了pycharm 2020 1.1的安装流程,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-09-09
  • Python 非极大值抑制(NMS)的四种实现详解

    Python 非极大值抑制(NMS)的四种实现详解

    本文主要介绍了非极大值抑制(Non-Maximum Suppression,NMS)的四种实现方式,不同方法对NMS速度的影响各不相同,感兴趣的小伙伴可以了解一下
    2021-11-11
  • 基于Python+Flask实现一个简易网页验证码登录系统案例

    基于Python+Flask实现一个简易网页验证码登录系统案例

    当今的互联网世界中,为了防止恶意访问,许多网站在登录和注册表单中都采用了验证码技术,验证码可以防止机器人自动提交表单,确保提交行为背后有一个真实的人类用户,本文将向您展示如何使用Python的Flask框架来创建一个简单的验证码登录系统
    2023-09-09

最新评论