用pyecharts绘制桑基图
时间: 2023-11-22 21:48:54 浏览: 141
以下是使用pyecharts绘制桑基图的示例代码和步骤:
1.安装pyecharts库
```shell
pip install pyecharts
```
2.导入所需模块
```python
from pyecharts import options as opts
from pyecharts.charts import Sankey
```
3.准备数据
```python
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": "category4", "target": "category5", "value": 25},
{"source": "category5", "target": "category6", "value": 30},
]
```
4.创建Sankey对象并设置属性
```python
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="Sankey-基本示例"))
)
```
5.生成HTML文件并在浏览器中查看
```python
sankey.render("sankey.html")
```
运行上述代码后,会在当前目录下生成一个名为“sankey.html”的文件,用浏览器打开即可查看生成的桑基图。
阅读全文