我想用pyecharts写一个5个层级的桑基图,请给我一个示例,节点与节点关系随机就行。
时间: 2024-05-07 18:16:24 浏览: 61
python+echarts+excel制作桑基图
5星 · 资源好评率100%
好的,以下是一个5层级的随机桑基图的示例代码:
```python
from pyecharts import options as opts
from pyecharts.charts import Sankey
nodes = [
{"name": "A1"},
{"name": "B1"},
{"name": "B2"},
{"name": "C1"},
{"name": "C2"},
{"name": "D1"},
{"name": "D2"},
{"name": "E1"},
{"name": "E2"},
]
links = [
{"source": "A1", "target": "B1", "value": 10},
{"source": "A1", "target": "B2", "value": 5},
{"source": "B1", "target": "C1", "value": 8},
{"source": "B1", "target": "C2", "value": 2},
{"source": "B2", "target": "C1", "value": 3},
{"source": "B2", "target": "C2", "value": 2},
{"source": "C1", "target": "D1", "value": 6},
{"source": "C1", "target": "D2", "value": 2},
{"source": "C2", "target": "D1", "value": 4},
{"source": "C2", "target": "D2", "value": 1},
{"source": "D1", "target": "E1", "value": 5},
{"source": "D1", "target": "E2", "value": 1},
{"source": "D2", "target": "E1", "value": 3},
{"source": "D2", "target": "E2", "value": 1},
]
sankey = (
Sankey()
.add(
series_name="",
nodes=nodes,
links=links,
itemstyle_opts=opts.ItemStyleOpts(border_width=1, border_color="#aaa"),
linestyle_opt=opts.LineStyleOpts(curve=0.5),
)
.set_global_opts(title_opts=opts.TitleOpts(title="5层级随机桑基图"))
)
sankey.render("5_level_sankey.html")
```
这段示例代码会生成一个5层级的随机桑基图,并将其保存为"5_level_sankey.html"文件。你可以自己修改节点和节点之间的关系来生成不同的图表。
阅读全文