Skip to content

Commit 7cc8fba

Browse files
committed
Update sankey-diagram.md
Add colorful links to the Sankey diagram.
1 parent b644fb5 commit 7cc8fba

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

doc/python/sankey-diagram.md

+10-2
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,13 @@ import urllib, json
7171
url = 'https://raw.githubusercontent.com/plotly/plotly.js/master/test/image/mocks/sankey_energy.json'
7272
response = urllib.request.urlopen(url)
7373
data = json.loads(response.read())
74+
75+
# override gray link colors with 'source' colors
76+
opacity = 0.4
77+
data['data'][0]['node']['color'] = ['rgba(255,0,255, 0.8)' if color == "magenta" else color for color in data['data'][0]['node']['color']]
78+
data['data'][0]['link']['color'] = [data['data'][0]['node']['color'][src] for src in data['data'][0]['link']['source']]
79+
data['data'][0]['link']['color'] = [color.replace("0.8", str(opacity)) for color in data['data'][0]['link']['color']]
80+
7481
fig = go.Figure(data=[go.Sankey(
7582
valueformat = ".0f",
7683
valuesuffix = "TWh",
@@ -87,8 +94,9 @@ fig = go.Figure(data=[go.Sankey(
8794
source = data['data'][0]['link']['source'],
8895
target = data['data'][0]['link']['target'],
8996
value = data['data'][0]['link']['value'],
90-
label = data['data'][0]['link']['label']
91-
))])
97+
label = data['data'][0]['link']['label'],
98+
color = data['data'][0]['link']['color']
99+
))])
92100

93101
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>",
94102
font_size=10)

0 commit comments

Comments
 (0)