@@ -218,42 +218,22 @@ Create a Sankey diagram with arrow links by setting the `arrowlen` attribute of
218
218
219
219
``` python
220
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
221
234
- fig = go.Figure(data = [go.Sankey(
235
- valueformat = " .0f" ,
236
- valuesuffix = " TWh" ,
237
- # Define nodes
222
+ fig = go.Figure(go.Sankey(
223
+ arrangement = " snap" ,
238
224
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' ]
225
+ label = [" A" , " B" , " C" , " D" , " E" , " F" ],
226
+ x = [0.2 , 0.1 , 0.5 , 0.7 , 0.3 , 0.5 ],
227
+ y = [0.7 , 0.5 , 0.2 , 0.4 , 0.2 , 0.3 ],
228
+ pad = 10
244
229
),
245
- # Add links
246
230
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
- ))])
231
+ arrowlen = 15 ,
232
+ source = [0 , 0 , 1 , 2 , 5 , 4 , 3 , 5 ],
233
+ target = [5 , 3 , 4 , 3 , 0 , 2 , 2 , 3 ],
234
+ value = [1 , 2 , 1 , 1 , 1 , 1 , 1 , 2 ]
235
+ )))
254
236
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
237
fig.show()
258
238
```
259
239
0 commit comments