利用jieba和pyecharts做新闻关键词统计可视化

pyecharts 是一个用于生成 Echarts 图表的类库。Echarts 是百度开源的一个数据可视化 JS 库。用 Echarts 生成的图可视化效果非常棒,为了与 Python 进行对接,方便在 Python 中直接使用数据生成图。

安装pyecharts、jieba

pip install pyecharts jieba

 详细代码

新闻原文链接

import jieba
import re
from collections import Counter
cut_words=""
for line in open('text',encoding='utf-8'):
    line.strip('\n')
    line = re.sub("[A-Za-z0-9\'\:\·\—\,\。\“ \”\n\u3000\?\、\'*\',\']", "", line)
    seg_list=jieba.cut(line,cut_all=False)
    cut_words+=(" ".join(seg_list))
all_words=cut_words.split()
# print(all_words)
c=Counter()
for x in all_words:
    if len(x)>1 and x != '\r\n':
        c[x] += 1

print('\n词频统计结果:')
dict={}
for (k,v) in c.most_common(10):# 输出词频最高的前10个词
    dict[k]=v
    # # dict['value']=v
    print("%s:%d"%(k,v))

keyList,valueList = [],[]
for k,v in dict.items():
    keyList.append(k)
    valueList.append(v)
print(keyList,valueList)

# 导入柱状图-Bar
from pyecharts.charts import Bar
bar = Bar()
bar.add_xaxis(keyList)
bar.add_yaxis("新关键字统计",valueList)
bar.render()

运行结果

柱状图结果

pychart文档https://pyecharts.org/#/zh-cn/quickstart

发布了375 篇原创文章 · 获赞 468 · 访问量 167万+

猜你喜欢

转载自blog.csdn.net/bbwangj/article/details/105074274