@@ -5,12 +5,12 @@ jupyter:
5
5
text_representation :
6
6
extension : .md
7
7
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
10
10
kernel_info :
11
11
name : python2
12
12
kernelspec :
13
- display_name : Python 3
13
+ display_name : Python 3 (ipykernel)
14
14
language : python
15
15
name : python3
16
16
language_info :
@@ -22,7 +22,7 @@ jupyter:
22
22
name : python
23
23
nbconvert_exporter : python
24
24
pygments_lexer : ipython3
25
- version : 3.7.6
25
+ version : 3.7.0
26
26
plotly :
27
27
description : How to make Sankey Diagrams in Python with Plotly.
28
28
display_as : basic
@@ -210,6 +210,53 @@ fig = go.Figure(go.Sankey(
210
210
fig.show()
211
211
```
212
212
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
+
213
260
### Reference
214
261
215
262
See [ https://plotly.com/python/reference/sankey ] ( https://plotly.com/python/reference/sankey/ ) for more information and options!
0 commit comments