用pyecharts写一个3层的桑基图,第一层节点为A1、A2和A3,大小分别为10、7和5,第二层为B1和B2,大小分别为9和3,第三层为C1和C2,大小分别为4和6。要求A1于B1连接,A2同时与B1和B2连接,B1和B2同时与C1连接,A3又和C2连接。
时间: 2024-06-03 22:09:23 浏览: 132
好的,以下是用pyecharts实现的代码:
```python
from pyecharts import options as opts
from pyecharts.charts import Sankey
nodes = [
{"name": "A1"},
{"name": "A2"},
{"name": "A3"},
{"name": "B1"},
{"name": "B2"},
{"name": "C1"},
{"name": "C2"},
]
links = [
{"source": "A1", "target": "B1", "value": 10},
{"source": "A2", "target": "B1", "value": 7},
{"source": "A2", "target": "B2", "value": 7},
{"source": "A3", "target": "C2", "value": 5},
{"source": "B1", "target": "C1", "value": 9},
{"source": "B2", "target": "C1", "value": 3},
{"source": "B1", "target": "C2", "value": 1},
{"source": "B2", "target": "C2", "value": 5},
]
sankey = (
Sankey()
.add(
"sankey",
nodes=nodes,
links=links,
itemstyle_opts=opts.ItemStyleOpts(border_width=1, border_color="#aaa"),
linestyle_opt=opts.LineStyleOpts(opacity=0.2, curve=0.5, color="source"),
label_opts=opts.LabelOpts(position="right"),
)
.set_global_opts(
title_opts=opts.TitleOpts(title="三层桑基图"),
tooltip_opts=opts.TooltipOpts(trigger_on="mousemove"),
)
.render("sankey.html")
)
```
生成的图如下:
![sankey](https://cdn.jsdelivr.net/gh/feiyuu/cdn/img/2022/sankey.png)
阅读全文