From f4ae58b08c6ae8cd45b37a203f791cf8a4a6ff10 Mon Sep 17 00:00:00 2001 From: Liam Connors Date: Tue, 16 Aug 2022 17:05:20 -0400 Subject: [PATCH 1/3] Update sankey-diagram.md --- doc/python/sankey-diagram.md | 55 +++++++++++++++++++++++++++++++++--- 1 file changed, 51 insertions(+), 4 deletions(-) diff --git a/doc/python/sankey-diagram.md b/doc/python/sankey-diagram.md index 2c37357513b..25687646eec 100644 --- a/doc/python/sankey-diagram.md +++ b/doc/python/sankey-diagram.md @@ -5,12 +5,12 @@ jupyter: text_representation: extension: .md format_name: markdown - format_version: '1.2' - jupytext_version: 1.6.0 + format_version: '1.3' + jupytext_version: 1.14.1 kernel_info: name: python2 kernelspec: - display_name: Python 3 + display_name: Python 3 (ipykernel) language: python name: python3 language_info: @@ -22,7 +22,7 @@ jupyter: name: python nbconvert_exporter: python pygments_lexer: ipython3 - version: 3.7.6 + version: 3.7.0 plotly: description: How to make Sankey Diagrams in Python with Plotly. display_as: basic @@ -210,6 +210,53 @@ fig = go.Figure(go.Sankey( fig.show() ``` +### Sankey Diagram with Arrow Links + +*New in 5.10* + +Create a Sankey diagram with arrow links by setting the `arrowlen` attribute of `link`: + +```python +import plotly.graph_objects as go +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 +# change 'magenta' to its 'rgba' value to add opacity +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].replace("0.8", str(opacity)) + for src in data['data'][0]['link']['source']] + +fig = go.Figure(data=[go.Sankey( + valueformat = ".0f", + valuesuffix = "TWh", + # Define nodes + node = dict( + pad = 15, + thickness = 15, + line = dict(color = "black", width = 0.5), + label = data['data'][0]['node']['label'], + color = data['data'][0]['node']['color'] + ), + # Add links + link = dict( + arrowlen = 15, + source = data['data'][0]['link']['source'], + target = data['data'][0]['link']['target'], + value = data['data'][0]['link']['value'], + label = data['data'][0]['link']['label'], + color = data['data'][0]['link']['color'] +))]) + +fig.update_layout(title_text="Energy forecast for 2050
Source: Department of Energy & Climate Change, Tom Counsell via Mike Bostock", + font_size=10) +fig.show() +``` + ### Reference See [https://plotly.com/python/reference/sankey](https://plotly.com/python/reference/sankey/) for more information and options! From 846a2861263b725ed6e34d39599ea36fb7c2f65e Mon Sep 17 00:00:00 2001 From: Liam Connors Date: Tue, 16 Aug 2022 17:14:12 -0400 Subject: [PATCH 2/3] Update sankey-diagram.md --- doc/python/sankey-diagram.md | 42 ++++++++++-------------------------- 1 file changed, 11 insertions(+), 31 deletions(-) diff --git a/doc/python/sankey-diagram.md b/doc/python/sankey-diagram.md index 25687646eec..f6462064292 100644 --- a/doc/python/sankey-diagram.md +++ b/doc/python/sankey-diagram.md @@ -218,42 +218,22 @@ Create a Sankey diagram with arrow links by setting the `arrowlen` attribute of ```python import plotly.graph_objects as go -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 -# change 'magenta' to its 'rgba' value to add opacity -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].replace("0.8", str(opacity)) - for src in data['data'][0]['link']['source']] -fig = go.Figure(data=[go.Sankey( - valueformat = ".0f", - valuesuffix = "TWh", - # Define nodes +fig = go.Figure(go.Sankey( + arrangement = "snap", node = dict( - pad = 15, - thickness = 15, - line = dict(color = "black", width = 0.5), - label = data['data'][0]['node']['label'], - color = data['data'][0]['node']['color'] + label = ["A", "B", "C", "D", "E", "F"], + x = [0.2, 0.1, 0.5, 0.7, 0.3, 0.5], + y = [0.7, 0.5, 0.2, 0.4, 0.2, 0.3], + pad = 10 ), - # Add links link = dict( - arrowlen = 15, - source = data['data'][0]['link']['source'], - target = data['data'][0]['link']['target'], - value = data['data'][0]['link']['value'], - label = data['data'][0]['link']['label'], - color = data['data'][0]['link']['color'] -))]) + arrowlen = 15, + source = [0, 0, 1, 2, 5, 4, 3, 5], + target = [5, 3, 4, 3, 0, 2, 2, 3], + value = [1, 2, 1, 1, 1, 1, 1, 2] + ))) -fig.update_layout(title_text="Energy forecast for 2050
Source: Department of Energy & Climate Change, Tom Counsell via Mike Bostock", - font_size=10) fig.show() ``` From 3d9aecece94dde393ed067d3c39930fb3b9a892b Mon Sep 17 00:00:00 2001 From: Liam Connors Date: Wed, 17 Aug 2022 08:33:24 -0400 Subject: [PATCH 3/3] Update doc/python/sankey-diagram.md Co-authored-by: Chris Parmer --- doc/python/sankey-diagram.md | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/doc/python/sankey-diagram.md b/doc/python/sankey-diagram.md index f6462064292..15bfe71dac3 100644 --- a/doc/python/sankey-diagram.md +++ b/doc/python/sankey-diagram.md @@ -220,19 +220,20 @@ Create a Sankey diagram with arrow links by setting the `arrowlen` attribute of import plotly.graph_objects as go fig = go.Figure(go.Sankey( - arrangement = "snap", - node = dict( - label = ["A", "B", "C", "D", "E", "F"], - x = [0.2, 0.1, 0.5, 0.7, 0.3, 0.5], - y = [0.7, 0.5, 0.2, 0.4, 0.2, 0.3], - pad = 10 + arrangement='snap', + node=dict( + label=['A', 'B', 'C', 'D', 'E', 'F'], + x=[0.2, 0.1, 0.5, 0.7, 0.3, 0.5], + y=[0.7, 0.5, 0.2, 0.4, 0.2, 0.3], + pad=10 ), - link = dict( - arrowlen = 15, - source = [0, 0, 1, 2, 5, 4, 3, 5], - target = [5, 3, 4, 3, 0, 2, 2, 3], - value = [1, 2, 1, 1, 1, 1, 1, 2] - ))) + link=dict( + arrowlen=15, + source=[0, 0, 1, 2, 5, 4, 3, 5], + target=[5, 3, 4, 3, 0, 2, 2, 3], + value=[1, 2, 1, 1, 1, 1, 1, 2] + ) +)) fig.show() ```