【词云图】从excel和从txt文件,绘制以句子、词为单位的词云图

从excel和从txt文件,绘制以句子、词为单位的词云图

写在最前面

经常绘制词云图,这次正好梳理一下,方便之后自己直接copy微调代码。

代码功能说明:

1、支持plt绘图的中文正常显示

2、以句为单位、和以词为单位进行词云图分析

3、支持excel的数据读取,注意excel中nan数据必须先进行处理,本文以'未提供'填充缺失数据。

4、支持txt文件的数据读取

数据说明&结论

这份数据包含了我最近发布的文章标题。

为了更好地分析这些数据,首先对数据进行清理和整理,然后进行可视化分析,并最后提出一些结论。

从txt文件,绘制以句子、词为单位的词云图

python 复制代码
import matplotlib.pyplot as plt
import seaborn as sns
from wordcloud import WordCloud
import jieba  # 导入 jieba 分词库

plt.rcParams['font.sans-serif'] = ['SimHei']  # 设置字体,以便支持中文
sns.set(style="whitegrid")  # 设置图表风格

# 生成词云的函数
def generate_wordcloud(text, title, use_jieba=False):
    plt.rcParams['font.sans-serif'] = ['SimHei']  # 设置字体,以便支持中文
    if use_jieba:
        text = " ".join(jieba.cut(text))  # 使用 jieba 进行分词

    wordcloud = WordCloud(width=800, height=400, font_path='simhei.ttf', background_color='white').generate(text)
    plt.figure(figsize=(10, 5))
    plt.imshow(wordcloud, interpolation='bilinear')
    plt.title(title, fontsize=18)
    plt.axis("off")
    plt.show()

# 读取 TXT 文件
file_path = '自我介绍.txt'  # 替换为您的 TXT 文件路径
with open(file_path, 'r', encoding='utf-8') as file:
    text = file.read()

title = '自我介绍'

# 生成词云
generate_wordcloud(text, title)
generate_wordcloud(text, title, use_jieba=True)

自我介绍


从excel,绘制以句子、词为单位的词云图

读取excel

python 复制代码
import matplotlib.pyplot as plt
import seaborn as sns
from wordcloud import WordCloud
import jieba

import pandas as pd

# Load the provided Excel file for analysis
file_path = 'score1.xlsx'
try:
    data = pd.read_excel(file_path)
except Exception as e:
    print(f"Error reading the Excel file: {e}")

# 处理数据
data.fillna('未提供', inplace=True)

# Displaying the first few rows of the dataset to understand its structure and contents
data.head()

| | 文章标题 | URL | 发布时间 | 阅读量 | 收藏量 | 点赞量 | 评论量 | 质量分 |
| 0 | 【力扣热题100】287. 寻找重复数(弗洛伊德的乌龟和兔子方法) | https://blog.csdn.net/wtyuong/article/details/... | 2023-12-08 23:39:32 | 596 | 17 | 24 | 14 | 91 |
| 1 | 2024年大学生考试/考证年历框架 | https://blog.csdn.net/wtyuong/article/details/... | 2023-12-08 23:24:47 | 368 | 11 | 12 | 2 | 90 |
| 2 | 【从0配置JAVA项目相关环境1】jdk + VSCode运行java + mysql + ... | https://blog.csdn.net/wtyuong/article/details/... | 2023-12-06 21:27:26 | 1936 | 39 | 60 | 104 | 97 |
| 3 | 【从0配置JAVA项目相关环境2】node.js + 前端 从配置到运行 | https://blog.csdn.net/wtyuong/article/details/... | 2023-12-06 21:26:33 | 1544 | 25 | 27 | 5 | 93 |

4 GPT-Crawler一键爬虫构建GPTs知识库 https://blog.csdn.net/wtyuong/article/details/... 2023-12-05 23:32:37 1815 30 31 29 94

绘制以句子、词为单位的词云图

python 复制代码
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from wordcloud import WordCloud
import jieba  # 导入jieba分词库

plt.rcParams['font.sans-serif'] = ['SimHei']  # 设置字体,以便支持中文
sns.set(style="whitegrid")  # 设置图表风格

# 生成词云的函数
def generate_wordcloud(text_series, title, use_jieba=False):
    plt.rcParams['font.sans-serif'] = ['SimHei']  # 设置字体,以便支持中文
    text = ' '.join(text_series.astype(str))  # 将文本转换为字符串
    if use_jieba:
        text = " ".join(jieba.cut(text))  # 使用jieba进行分词

    wordcloud = WordCloud(width=800, height=400, font_path='simhei.ttf', background_color='white').generate(text)
    plt.figure(figsize=(10, 5))
    plt.imshow(wordcloud, interpolation='bilinear')
    plt.title(title, fontsize=18)
    plt.axis("off")
    plt.show()

# 对不同的列生成词云
generate_wordcloud(data['文章标题'], '标题')
generate_wordcloud(data['文章标题'], '标题', use_jieba=True)

文章标题


相关推荐
Hy行者勇哥1 小时前
Python 与 VS Code 结合操作指南
开发语言·python
大力水手(Popeye)1 小时前
Pytorch——tensor
人工智能·pytorch·python
饕餮争锋2 小时前
设计模式笔记_行为型_访问者模式
笔记·设计模式·访问者模式
不羁。。4 小时前
【撸靶笔记】第七关:GET - Dump into outfile - String
数据库·笔记·oracle
飞翔的佩奇5 小时前
【完整源码+数据集+部署教程】表盘指针检测系统源码和数据集:改进yolo11-CA-HSFPN
python·yolo·计算机视觉·数据集·yolo11·表盘指针检测
larance6 小时前
SQLAlchemy 的异步操作来批量保存对象列表
数据库·python
搏博6 小时前
基于Python3.10.6与jieba库的中文分词模型接口在Windows Server 2022上的实现与部署教程
windows·python·自然语言处理·flask·中文分词
lxmyzzs7 小时前
pyqt5无法显示opencv绘制文本和掩码信息
python·qt·opencv
萧鼎8 小时前
Python pyzmq 库详解:从入门到高性能分布式通信
开发语言·分布式·python
好望角雾眠9 小时前
第一阶段C#基础-10:集合(Arraylist,list,Dictionary等)
笔记·学习·c#