阿里云数据读取做桑基图

本文介绍了一种使用PyODPS、Pandas和PyEcharts进行流量数据分析并绘制桑基图的方法,展示了如何从ODPS读取数据,转换为Pandas DataFrame,然后利用PyEcharts生成日活流向的桑基图,最后将图表保存为本地HTML文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

        主要自己做个记录吧,流程:pyodps读取数据源-->pandas-->pyecharts;每天调度会计算好数据落到odps,这边再加个自动发送就好了。

桑基图个人觉得某些场景还是蛮适合的,图像的表征能力还是很强的。

2022年6月1日记录;搞流量数据之后发现这个表有一个好处,做页面周转的图可太好了,记录当前页面从哪里来,到哪里去,之前怎么没想到呢。

画图部分主要参考了这个https://blog.youkuaiyun.com/akenseren/article/details/103416477,中间细节部分还是看了下官方文档。

# -*- coding: utf-8 -*-
import pandas as pd
from pyecharts.charts import Sankey
from pyecharts import options as opts
import logging
import logging.config
from odps import ODPS
from odps.df import DataFrame


o = ODPS(
    access_id='********',
    secret_access_key='*********',
    project='*******',
    endpoint='http://service.odps.aliyun.com/api'
)


# logging.basicConfig(
#     format='%(name)s:%(lineno)d - %(levelname)s - %(message)s',
#     level=logging.DEBUG)

# 读取csv文件
#data = pd.read_excel('/Users/wangyuhang/Downloads/test.xlsx',sheet_name= 'Sheet2',header=None)

data = DataFrame(o.get_table('tmp_download_table')).to_pandas()
print(data.head(5))

# 生成nodes
nodes = []

dataNew = pd.concat([data['first_level'],data['second_level']],axis=0,ignore_index=True).unique()
print(dataNew)

for i in dataNew:
    dic = {}
    dic['name'] = i
    nodes.append(dic)
print(nodes)
# for j in data[1].unique:
#     dic['name'] = i
#     nodes.append(dic)

# 生成links
links = []
for i in data.values:
    dic = {}
    dic['source'] = i[0]
    dic['target'] = i[1]
    dic['value'] = i[2]
    links.append(dic)

print(links)
# pyecharts 所有方法均支持链式调用。

c = (
        Sankey(init_opts=opts.InitOpts(width="1200px", height="600px"))
        .add(
"uv",
            nodes,
            links,
            linestyle_opt=opts.LineStyleOpts(opacity=0.2, curve=0.5, color="source",type_="dotted"),
            label_opts=opts.LabelOpts(position="right",),
        )
        .set_global_opts(title_opts=opts.TitleOpts(title="日活流向"))
    )
# 输出html可视化结果
c.render('/Users/wangyuhang/Downloads/result11.html')

这样输出一个本地的html文件,直接打开这个文件就是图:

细节上还有很多可以补充的,之后改了再加吧

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值