用pyecharts写一个3层的桑基图,第一层节点为A1和A2,大小分别为10和5,第二层为B1和B2,大小分别为9和3,第三层为C1和C2,大小分别为4和6。要求A1于B1连接,A2同时与B1和B2连接,B1和B2同时与C1连接,A2又和C2连接。
时间: 2024-05-04 22:15:42 浏览: 122
好的,这是一个关于pyecharts的问题,我可以回答它。以下是代码:
```
from pyecharts import options as opts
from pyecharts.charts import Sankey
nodes = [
{"name": "A1"},
{"name": "A2"},
{"name": "B1"},
{"name": "B2"},
{"name": "C1"},
{"name": "C2"},
]
links = [
{"source": "A1", "target": "B1", "value": 10},
{"source": "A2", "target": "B1", "value": 5},
{"source": "A2", "target": "B2", "value": 5},
{"source": "B1", "target": "C1", "value": 9},
{"source": "B1", "target": "C2", "value": 5},
{"source": "B2", "target": "C1", "value": 3},
{"source": "B2", "target": "C2", "value": 5},
]
sankey = (
Sankey()
.add("sankey", nodes, links, 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="3层桑基图"))
)
sankey.render("sankey.html")
```
你可以将代码复制到Jupyter Notebook或py文件中运行,然后会生成一个名为sankey.html的文件,打开它就可以看到3层桑基图了。
阅读全文