Skip to content

Commit

Permalink
Update sankey-diagram.md
Browse files Browse the repository at this point in the history
Add colorful links to the Sankey diagram.
  • Loading branch information
SylwiaOliwia2 authored Mar 19, 2020
1 parent c56725a commit 40abee6
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions doc/python/sankey-diagram.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,17 @@ import urllib, json
url = 'https://raw.githubusercontent.com/plotly/plotly.js/master/test/image/mocks/sankey_energy.json'
response = urllib.request.urlopen(url)
data = json.loads(response.read())

# override gray link colors with 'source' colors
opacity = 0.4
data['data'][0]['node']['color'] = ['rgba(255,0,255, 0.8)' if color == "magenta" else color for color in data['data'][0]['node']['color']]
data['data'][0]['link']['color'] = [data['data'][0]['node']['color'][src] for src in data['data'][0]['link']['source']]
data['data'][0]['link']['color'] = [color.replace("0.8", str(opacity)) for color in data['data'][0]['link']['color']]

fig = go.Figure(data=[go.Sankey(
valueformat = ".0f",
valuesuffix = "TWh",
# Define nodes
# Define nodes
node = dict(
pad = 15,
thickness = 15,
Expand All @@ -87,8 +94,9 @@ fig = go.Figure(data=[go.Sankey(
source = data['data'][0]['link']['source'],
target = data['data'][0]['link']['target'],
value = data['data'][0]['link']['value'],
label = data['data'][0]['link']['label']
))])
label = data['data'][0]['link']['label'],
color = data['data'][0]['link']['color']
))])

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>",
font_size=10)
Expand Down

0 comments on commit 40abee6

Please sign in to comment.