pyecharts 桑基图 节点位置 调整 对应pyecharts2.0.3版本
时间: 2023-11-15 13:11:42 浏览: 150
在 Pyecharts 2.0.3 版本中,可以通过 `sankey_node_pos` 参数调整桑基图中节点的位置。
具体操作步骤如下:
1. 导入 `sankey` 类和 `sankey_node_pos` 参数:
```python
from pyecharts.charts import Sankey
from pyecharts.options import sankey_node_pos
```
2. 定义节点位置:
```python
node_pos = [
{"name": "A", "x": 50, "y": 50},
{"name": "B", "x": 100, "y": 100},
{"name": "C", "x": 150, "y": 50},
{"name": "D", "x": 200, "y": 100},
]
```
其中,`name` 表示节点名称,`x` 和 `y` 表示节点在桑基图中的位置。
3. 创建桑基图对象并传入节点位置:
```python
sankey = Sankey()
sankey.add("sankey", nodes, links,
linestyle_opt=opts.LineStyleOpts(opacity=0.2, curve=0.5, color="source"),
label_opts=opts.LabelOpts(position="right"),
node_gap=20,
sankey_node_pos=node_pos)
```
完整代码示例:
```python
from pyecharts.charts import Sankey
from pyecharts.options import sankey_node_pos
from pyecharts import options as opts
nodes = [
{"name": "A"},
{"name": "B"},
{"name": "C"},
{"name": "D"},
]
links = [
{"source": "A", "target": "B", "value": 10},
{"source": "B", "target": "C", "value": 15},
{"source": "C", "target": "D", "value": 20},
]
node_pos = [
{"name": "A", "x": 50, "y": 50},
{"name": "B", "x": 100, "y": 100},
{"name": "C", "x": 150, "y": 50},
{"name": "D", "x": 200, "y": 100},
]
sankey = Sankey()
sankey.add("sankey", nodes, links,
linestyle_opt=opts.LineStyleOpts(opacity=0.2, curve=0.5, color="source"),
label_opts=opts.LabelOpts(position="right"),
node_gap=20,
sankey_node_pos=node_pos)
sankey.render("sankey.html")
```
阅读全文