python实现词云

python实现词云的方式有很多种,例如wordcloud包、pyecharts包、stylecloud包,这里主要介绍两种办法:

  • 调用pyecharts包画图
  • 调用stylecloud包画图

pyecharts包画词云图

明白要输入什么数据和怎么调整配置就行,比如这里要输入的是数组,数组里面是字典格式key-value,那么我们就根据要求组出数据给包执行就行。

  • 基本代码如下
# -*- coding: utf-8 -*-
# @Time : 2022/5/1 16:22
# @Author : MinChess
# @File : wordcloud.py
# @Software: PyCharm
from pyecharts import options as opts
from pyecharts.charts import WordCloud
from pyecharts.globals import SymbolType

# 数据
words = [
    ('背包问题', 10000),
    ('大整数', 6181),
    ('Karatsuba乘法算法', 4386),
    ('穷举搜索', 4055),
    ('傅里叶变换', 2467),
    ('状态树遍历', 2244),
    ('剪枝', 1868),
    ('Gale-shapley', 1484),
    ('最大匹配与匈牙利算法', 1112),
    ('线索模型', 865),
    ('关键路径算法', 847),
    ('最小二乘法曲线拟合', 582),
    ('二分逼近法', 555),
    ('牛顿迭代法', 550),
    ('Bresenham算法', 462),
    ('粒子群优化', 366),
    ('Dijkstra', 360),
    ('A*算法', 282),
    ('负极大极搜索算法', 273),
    ('估值函数', 265)
]

# 渲染图
def wordcloud_base() -> WordCloud:
    c = (
        WordCloud()
        .add("", words, word_size_range=[20, 100], shape='diamond')  # SymbolType.ROUND_RECT
        .set_global_opts(title_opts=opts.TitleOpts(title='WordCloud词云'))
    )
    return c

# 生成图
wordcloud_base().render('词云图.html')

  • 输出为一个网页文件,打开就能查看了
image-20220501162435228
  • 文本分词并统计
# coding=utf-8
import jieba
import re
import time
from collections import Counter
from pyecharts import options as opts
from pyecharts.charts import WordCloud
from pyecharts.globals import SymbolType


cut_words = ""
all_words = ""
for line in open('文本.txt', encoding='utf-8'):
    line.strip('\n')
    seg_list = jieba.cut(line,cut_all=False)
    cut_words = (" ".join(seg_list))
    all_words += cut_words

c = Counter()
for x in all_words:
    if len(x)>1 and x != '\r\n':
        c[x] += 1


words = []
for (k,v) in c.most_common(1000):
    # print(k, v)
    words.append((k,v))

# 渲染图
def wordcloud_base() -> WordCloud:
    c = (
        WordCloud()
        .add("", words, word_size_range=[20, 100], shape=SymbolType.ROUND_RECT)
        .set_global_opts(title_opts=opts.TitleOpts(title='词云图'))
    )
    return c

# 生成图
wordcloud_base().render('词云图.html')

stylecloud画图

stylecloud画的图很好看,无论是颜色还是图案都非常好看,如下图:

更多内容可以查看:

image-20220501162435228

在线工具