Skip to content

Commit f4ae58b

Browse files
committed
Update sankey-diagram.md
1 parent 00c18fd commit f4ae58b

File tree

1 file changed

+51
-4
lines changed

1 file changed

+51
-4
lines changed

Diff for: doc/python/sankey-diagram.md

+51-4
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ jupyter:
55
text_representation:
66
extension: .md
77
format_name: markdown
8-
format_version: '1.2'
9-
jupytext_version: 1.6.0
8+
format_version: '1.3'
9+
jupytext_version: 1.14.1
1010
kernel_info:
1111
name: python2
1212
kernelspec:
13-
display_name: Python 3
13+
display_name: Python 3 (ipykernel)
1414
language: python
1515
name: python3
1616
language_info:
@@ -22,7 +22,7 @@ jupyter:
2222
name: python
2323
nbconvert_exporter: python
2424
pygments_lexer: ipython3
25-
version: 3.7.6
25+
version: 3.7.0
2626
plotly:
2727
description: How to make Sankey Diagrams in Python with Plotly.
2828
display_as: basic
@@ -210,6 +210,53 @@ fig = go.Figure(go.Sankey(
210210
fig.show()
211211
```
212212

213+
### Sankey Diagram with Arrow Links
214+
215+
*New in 5.10*
216+
217+
Create a Sankey diagram with arrow links by setting the `arrowlen` attribute of `link`:
218+
219+
```python
220+
import plotly.graph_objects as go
221+
import urllib, json
222+
223+
url = 'https://raw.githubusercontent.com/plotly/plotly.js/master/test/image/mocks/sankey_energy.json'
224+
response = urllib.request.urlopen(url)
225+
data = json.loads(response.read())
226+
227+
# override gray link colors with 'source' colors
228+
opacity = 0.4
229+
# change 'magenta' to its 'rgba' value to add opacity
230+
data['data'][0]['node']['color'] = ['rgba(255,0,255, 0.8)' if color == "magenta" else color for color in data['data'][0]['node']['color']]
231+
data['data'][0]['link']['color'] = [data['data'][0]['node']['color'][src].replace("0.8", str(opacity))
232+
for src in data['data'][0]['link']['source']]
233+
234+
fig = go.Figure(data=[go.Sankey(
235+
valueformat = ".0f",
236+
valuesuffix = "TWh",
237+
# Define nodes
238+
node = dict(
239+
pad = 15,
240+
thickness = 15,
241+
line = dict(color = "black", width = 0.5),
242+
label = data['data'][0]['node']['label'],
243+
color = data['data'][0]['node']['color']
244+
),
245+
# Add links
246+
link = dict(
247+
arrowlen = 15,
248+
source = data['data'][0]['link']['source'],
249+
target = data['data'][0]['link']['target'],
250+
value = data['data'][0]['link']['value'],
251+
label = data['data'][0]['link']['label'],
252+
color = data['data'][0]['link']['color']
253+
))])
254+
255+
fig.update_layout(title_text="Energy forecast for 2050<br>Source: Department of Energy & Climate Change, Tom Counsell via <a href='https://bost.ocks.org/mike/sankey/'>Mike Bostock</a>",
256+
font_size=10)
257+
fig.show()
258+
```
259+
213260
### Reference
214261

215262
See [https://plotly.com/python/reference/sankey](https://plotly.com/python/reference/sankey/) for more information and options!

0 commit comments

Comments
 (0)