13.pyecharts桑基图

一、适用条件
1、桑基图:用于查看用户的行为,需要对产品流程有所了解,比如客户的引导与流失,这里画一个比较简单的,也是官网的例子。
二、代码实现
1.导入所需包

from pyecharts import options as opts
from pyecharts.charts import Sankey
from pyecharts.render import make_snapshot
#from snapshot_phantomjs import snapshot
from snapshot_pyppeteer import snapshot

2.数据整理

nodes = [
    {"name": "category1"},
    {"name": "category2"},
    {"name": "category3"},
    {"name": "category4"},
    {"name": "category5"},
    {"name": "category6"},
]

links = [
    {"source": "category1", "target": "category2", "value": 10},
    {"source": "category2", "target": "category3", "value": 15},
    {"source": "category3", "target": "category4", "value": 20},
    {"source": "category5", "target": "category6", "value": 25},
]

3 .桑基图

def Sankey_chart() -> Sankey:
    ################## 这部分可以直接用,保存成网页
    c= (
        Sankey(init_opts=opts.InitOpts(width="1000px", height="500px"))
        .add(
            series_name="",
            nodes=nodes,
            links=links,
            itemstyle_opts=opts.ItemStyleOpts(border_width=1, border_color="#aaa"),
            linestyle_opt=opts.LineStyleOpts(color="source", curve=0.5, opacity=0.5),
            tooltip_opts=opts.TooltipOpts(trigger_on="mousemove"),
        )
        .set_global_opts(title_opts=opts.TitleOpts(title="Sankey Diagram"))
        # .render("1.html")
    )
    ####################
    return c
make_snapshot(snapshot, Sankey_chart().render(), "13_1.gif")
if __name__ == '__main__':
    Sankey_chart()
13_1.gif

你可能感兴趣的:(13.pyecharts桑基图)