python绘制主题河流图

主题河流图一般在文本中用到的不多,但通过这种时间序列关系,我们能够分析一些特定的词在全文中的分布规律,这样有助于我们理解全文内容。

import re
import jieba
import pandas as pd
import openpyxl
import pyecharts.options as opts
from pyecharts.charts import ThemeRiver

def find(word,words,num):
    list = []
    sum_num = len(words)
    step = int(sum_num / num)
    for i in range(num):
        total = 0
        list_1=[i]
        new_list = words [i*step:(i+1)*step]
        for new_word in new_list:
            if new_word==word :
                total+=1
        list_1.append(total)
        list_1.append(word)
        list.append(list_1)
    return list

txt=open("C:/Users/Elegantmadman/Desktop/1.txt",'r').read()
words=jieba.lcut(txt)
count={}
sum = len(words)
for word in words:
    if len(word)==1:
        continue
    else:
        count[word]=count.get(word,0)+1
items=list(count.items())
items.sort(key=lambda x:x[-1],reverse=True)
x_data=[]
y_data=[]
for word in range(5):
    x_data.append(items[word][0])
for ci in x_data:
    y = find(ci, words, 20)
    for i in y:
        y_data.append(i)
(
    ThemeRiver(init_opts=opts.InitOpts(width="1600px", height="800px"))
    .add(
        series_name=x_data,
        data=y_data,
        label_opts=opts.LabelOpts(font_size = 18),
        singleaxis_opts=opts.SingleAxisOpts(
            pos_top="50", pos_bottom="50"
        ),
    )
    .set_global_opts(
        tooltip_opts=opts.TooltipOpts(trigger="axis", axis_pointer_type="line")
    )
    .render("C:/Users/Elegantmadman/Desktop/theme_river.html")
)

效果如下所示:
python绘制主题河流图_第1张图片

你可能感兴趣的:(python绘制主题河流图)