Python绘制词云图并统计词频

前言

词云图又叫文字云,是对文本数据中出现频率较高的关键词予以视觉上的突出,形成"关键词的渲染"就类似云一样的彩色图片,从而过滤掉大量的文本信息,使人一眼就可以领略文本数据的主要表达意思。
下面就用Python借助wordcloud库生成指定图形形状的词云图,并对数据进行统计。代码等相关文件可以去我的GitHub网站下载。

程序

1. 读取文件

def read_file(file_name):
    if os.path.exists(file_name):  # 判断文件是否存在
        with open(file_name, "r", encoding='utf-8') as file:  # 读取文件
            content = file.read()
            if content:  # 判断文本内容是否为空
                return content
            else:
                print("文件无内容")
    else:
        print(file_name, "文件不存在")
        

2. 词频统计

def statistics_words(content, number, word_statis_name):
    not_statis = ['也', '我', '着', '那', '这', '了', '你', ':', ':', '?', '!', '...', '…', '他', '她', '?', ',', '、', '。', '的', '和', '\u3000', '“', '”', ' ', 'ri', '与', '是', '在', '中', '了', '\n']
    words = list(jieba.lcut(content))
    statistics = Counter(words).most_common(number)
    dic = {key: value for (key, value) in statistics}
    for i in list(dic.keys()):
        if i in not_statis:
            dic.pop(i)

    label = list(dic.keys())
    N = len(dic)
    theta = np.arange(0.0, 2*np.pi, 2*np.pi/N)
    radii = list(dic.values())
    width = np.pi/6
    ax = plt.subplot(111, projection='polar')
    bars = ax.bar(theta, radii, width=width, bottom=0.0)
    plt.xticks(theta+np.pi/12, label)
    for r, bar in zip(radii, bars):
        bar.set_facecolor(plt.cm.viridis(r / 10.))
        bar.set_alpha(0.5)
    plt.savefig(word_statis_name)
    plt.show()
    

3. 生成词云图

def generate_word_cloud_illustration(content, figure_name, result_name):
        cut_text = jieba.cut(content)
        words = " ".join(cut_text)  # 拼接
        img = np.array(Image.open(figure_name))  # 读取图片
        img_colors = ImageColorGenerator(img)
        wd = wordcloud.WordCloud(mask=img, font_path="simhei.ttf", background_color="white")
        # wd.generate(words)
        wd.generate_from_text(words)
        plt.imshow(wd.recolor(color_func=img_colors), interpolation="bilinear")
        plt.axis("off")
        plt.savefig(result_name)
        plt.show()
        

4. main函数以及导入的包

import os
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image  # 导入PIL模块中的Image对象
import jieba           # 导入中文分词组件
import wordcloud       # 导入词云模块
from wordcloud import ImageColorGenerator
from collections import Counter
plt.rcParams['font.sans-serif'] = ['SimHei']  # 用来正常显示中文标签


if __name__ == '__main__':
    #file_name = "story.txt"     # 预读取文本文件名
    file_name = 'zhuxian.txt'
    figure_name = "map.png"     # 词频图形状图片文件名
    result_name = 'result.png'  # 保存的词云图文件名
    word_statis_name = "word_statistics"  # 词频统计绘制的雷达图文件名
    content = read_file(file_name)        # 读取文件
    statistics_words(content, 35, word_statis_name)  # 词频统计并绘制雷达图
    generate_word_cloud_illustration(content, figure_name, result_name)  # 生成词云图

结果

1. 词频统计

在这里插入图片描述

2. 词云图

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值