pyecharts桑基图
时间: 2023-09-10 11:10:40 浏览: 60
Pyecharts 是一个基于 Python 的开源数据可视化库,它支持多种类型的图表,包括柱状图、折线图、散点图、地图等等,其中也包括桑基图。桑基图是一种流程图,用于显示数据的流动情况,通常用于表示数据的来源、流动方向和量的大小。
以下是使用 Pyecharts 绘制桑基图的一个简单示例:
```python
from pyecharts.charts import Sankey
from pyecharts import options as opts
# 准备数据
nodes = [
{"name": "A"},
{"name": "B"},
{"name": "C"},
{"name": "D"},
{"name": "E"},
]
links = [
{"source": "A", "target": "B", "value": 10},
{"source": "A", "target": "C", "value": 15},
{"source": "B", "target": "D", "value": 12},
{"source": "C", "target": "D", "value": 8},
{"source": "D", "target": "E", "value": 20},
]
# 绘制桑基图
sankey = Sankey()
sankey.add(
"sankey",
nodes,
links,
linestyle_opt=opts.LineStyleOpts(opacity=0.5, curve=0.5, color="source"),
label_opts=opts.LabelOpts(position="right"),
)
sankey.set_global_opts(title_opts=opts.TitleOpts(title="Sankey Diagram"))
sankey.render("sankey.html")
```
上述代码中,我们首先准备了节点和链接数据,然后通过 `Sankey` 类构建了一个桑基图对象,最后使用 `add` 方法添加节点和链接数据,设置样式和标签选项,然后使用 `set_global_opts` 方法设置全局选项,最后通过 `render` 方法将图表渲染为 HTML 文件。