Wordcloud也能生成一个,带html的词云图文件吗??

-------------词云图集合-------------

WordCloud去掉停用词(fit_words+generate)的2种用法

通过词频来绘制词云图(jieba+WordCloud)

Python教程95:去掉停用词+词频统计+jieba.tokenize示例用法

将进酒---李白process_text词频统计,及词频可视化分析

使用wordcloud模块,绘制一个自定义的词云图形状

使用WordCloud模块中repeat参数,做一个关键字重复的词云图

关于词云图显示异常,出现乱码的解决办法

盘点WordCloud模块,词云图的相关知识点

Python源码05:使用Pyecharts画词云图图

1.简单的说一下实现思路:运行下面代码后,会生成一个名为wordcloud.html的文件,供后端使用。用浏览器打开这个文件,你会看到类似生成如下的词云图。这个示例代码通过Python生成词云图,并将图像转换为base64编码嵌入到HTML文件中,从而实现将词云图直接展示在网页上的效果。

python 复制代码
# -*- coding: utf-8 -*-
# @Author : 小红牛
# 微信公众号:WdPython
import io
import base64
from wordcloud import WordCloud
import matplotlib.pyplot as plt

# 1.准备一个词频字典
word_freq = {
    '李白': 700,
    '杜甫': 700,
    '李贺': 400,
    '苏东坡': 550,
    '杨过': 160,
    '张三丰': 100,
    '鬼谷子': 200,
    '韩信': 800,
    '姜尚': 900,
    '满江红': 90,
    '葬花吟': 50,
}
# 2.生成词云图的时候,如果含有中文的字,一定要添加一个中文的字体
# 如simkai.ttf为楷体
w = WordCloud(width=800, height=400, background_color='white',
              colormap='plasma', font_path='simkai.ttf')

w.fit_words(word_freq)

# 3.创建一个临时文件来保存图像
buffer = io.BytesIO()
plt.figure(figsize=(10, 5))
plt.imshow(w, interpolation='bilinear')
plt.axis('off')  # 关闭坐标轴
plt.savefig(buffer, format='png')
plt.close()

# 4.将图像转换为base64编码
buffer.seek(0)
img_str = base64.b64encode(buffer.read()).decode('utf-8')

# 5.创建HTML文件内容
html_content = f"""
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>我的Python教程</title>
    <style>
        body {{
            text-align: center;
            padding-top: 50px;
            background-color: #f0f0f0;
        }}
        img {{
            max-width: 100%;
            height: auto;
        }}
    </style>
</head>
<body>
    <h1>------中文词云图------</h1>
    <img src="data:image/png;base64,{img_str}" alt="Word Cloud">
</body>
</html>
"""

# 将HTML内容写入文件
with open('wordcloud.html', 'w', encoding='utf-8') as file:
    file.write(html_content)

print('wordcloud.html文件已经生成!!')

完毕!!感谢您的收看

----------★★历史博文集合★★----------

我的零基础Python教程,Python入门篇 进阶篇 视频教程 Py安装py项目 Python模块 Python爬虫 Json Xpath 正则表达式 Selenium Etree CssGui程序开发 Tkinter Pyqt5 列表元组字典数据可视化 matplotlib 词云图 Pyecharts 海龟画图 Pandas Bug处理 电脑小知识office自动化办公 编程工具 NumPy Pygame

相关推荐
1941s7 小时前
Google Agent Development Kit (ADK) 指南 第五章:工具集成与自定义
人工智能·python·langchain·agent·adk
故事和你917 小时前
sdut-python-实验四-python序列结构(21-27)
大数据·开发语言·数据结构·python·算法
chushiyunen7 小时前
pycharm注意力残差示例
ide·python·pycharm
2301_793804697 小时前
用Python和Twilio构建短信通知系统
jvm·数据库·python
B站_计算机毕业设计之家7 小时前
计算机毕业设计:Python当当网图书数据全链路处理平台 Django框架 爬虫 Pandas 可视化 大数据 大模型 书籍(建议收藏)✅
爬虫·python·机器学习·django·flask·pandas·课程设计
不要秃头的小孩7 小时前
力扣刷题——111.二叉树的最小深度
数据结构·python·算法·leetcode
我是鶸8 小时前
secml-malware python library 源码分析及实践
开发语言·python
进击的小头8 小时前
第15篇:MPC的发展方向及展望
python·算法
SugarFreeOixi8 小时前
MATLAB绘图风格记录NP类型
python·matlab·numpy
冥王丁B9 小时前
第31章 Prompt 与聊天模型笔记
笔记·python·prompt