Skip to content

Commit e982ec6

Browse files
Merge branch 'sankey-customdata'
2 parents 50846ad + 8d4be8b commit e982ec6

File tree

1 file changed

+35
-3
lines changed

1 file changed

+35
-3
lines changed

doc/python/sankey-diagram.md

+35-3
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ jupyter:
55
text_representation:
66
extension: .md
77
format_name: markdown
8-
format_version: '1.1'
9-
jupytext_version: 1.2.1
8+
format_version: '1.2'
9+
jupytext_version: 1.3.1
1010
kernel_info:
1111
name: python2
1212
kernelspec:
@@ -22,7 +22,7 @@ jupyter:
2222
name: python
2323
nbconvert_exporter: python
2424
pygments_lexer: ipython3
25-
version: 3.7.3
25+
version: 3.6.8
2626
plotly:
2727
description: How to make Sankey Diagrams in Python with Plotly.
2828
display_as: basic
@@ -143,6 +143,38 @@ fig.update_layout(
143143
fig.show()
144144
```
145145

146+
### Hovertemplate and customdata of Sankey diagrams
147+
148+
Links and nodes have their own hovertemplate, in which link- or node-specific attributes can be displayed. To add more data to links and nodes, it is possible to use the `customdata` attribute of `link` and `nodes`, as in the following example. For more information about hovertemplate and customdata, please see the [tutorial on hover text](/python/hover-text-and-formatting/).
149+
150+
```python
151+
import plotly.graph_objects as go
152+
153+
fig = go.Figure(data=[go.Sankey(
154+
node = dict(
155+
pad = 15,
156+
thickness = 20,
157+
line = dict(color = "black", width = 0.5),
158+
label = ["A1", "A2", "B1", "B2", "C1", "C2"],
159+
customdata = ["Long name A1", "Long name A2", "Long name B1", "Long name B2",
160+
"Long name C1", "Long name C2"],
161+
hovertemplate='Node %{customdata} has total value %{value}<extra></extra>',
162+
color = "blue"
163+
),
164+
link = dict(
165+
source = [0, 1, 0, 2, 3, 3], # indices correspond to labels, eg A1, A2, A2, B1, ...
166+
target = [2, 3, 3, 4, 4, 5],
167+
value = [8, 4, 2, 8, 4, 2],
168+
customdata = ["q","r","s","t","u","v"],
169+
hovertemplate='Link from node %{source.customdata}<br />'+
170+
'to node%{target.customdata}<br />has value %{value}'+
171+
'<br />and data %{customdata}<extra></extra>',
172+
))])
173+
174+
fig.update_layout(title_text="Basic Sankey Diagram", font_size=10)
175+
fig.show()
176+
```
177+
146178
### Define Node Position
147179

148180
The following example sets [node.x](https://plotly.com/python/reference/#sankey-node-x) and `node.y` to place nodes in the specified locations, except in the `snap arrangement` (default behaviour when `node.x` and `node.y` are not defined) to avoid overlapping of the nodes, therefore, an automatic snapping of elements will be set to define the padding between nodes via [nodepad](https://plotly.com/python/reference/#sankey-node-pad). The other possible arrangements are:<font color='blue'> 1)</font> perpendicular <font color='blue'>2)</font> freeform <font color='blue'>3)</font> fixed

0 commit comments

Comments
 (0)