Skip to content

Commit 991a6b2

Browse files
committed
sankey-diagram notebook
1 parent 2c8e46d commit 991a6b2

File tree

1 file changed

+44
-170
lines changed

1 file changed

+44
-170
lines changed

notebooks/sankey-diagram.md

Lines changed: 44 additions & 170 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
---
22
jupyter:
33
jupytext:
4+
notebook_metadata_filter: all
45
text_representation:
56
extension: .md
67
format_name: markdown
@@ -9,9 +10,19 @@ jupyter:
910
kernel_info:
1011
name: python2
1112
kernelspec:
12-
display_name: Python 2
13+
display_name: Python 3
1314
language: python
14-
name: python2
15+
name: python3
16+
language_info:
17+
codemirror_mode:
18+
name: ipython
19+
version: 3
20+
file_extension: .py
21+
mimetype: text/x-python
22+
name: python
23+
nbconvert_exporter: python
24+
pygments_lexer: ipython3
25+
version: 3.6.7
1526
plotly:
1627
description: How to make Sankey Diagrams in Python with Plotly.
1728
display_as: basic
@@ -27,198 +38,86 @@ jupyter:
2738
title: Sankey Diagram | Plotly
2839
---
2940

30-
#### New to Plotly?
31-
Plotly's Python library is free and open source! [Get started](https://plot.ly/python/getting-started/) by downloading the client and [reading the primer](https://plot.ly/python/getting-started/).
32-
<br>You can set up Plotly to work in [online](https://plot.ly/python/getting-started/#initialization-for-online-plotting) or [offline](https://plot.ly/python/getting-started/#initialization-for-offline-plotting) mode, or in [jupyter notebooks](https://plot.ly/python/getting-started/#start-plotting-online).
33-
<br>We also have a quick-reference [cheatsheet](https://images.plot.ly/plotly-documentation/images/python_cheat_sheet.pdf) (new!) to help you get started!
34-
41+
A [Sankey diagram](https://en.wikipedia.org/wiki/Sankey_diagram) is a flow diagram, in which the width of arrows is proportional to the flow quantity.
3542

36-
#### Version Check
37-
Plotly's python package is updated frequently. Run `pip install plotly --upgrade` to use the latest version.
38-
39-
```python inputHidden=false outputHidden=false
40-
import plotly
41-
plotly.__version__
42-
```
4343

4444
### Basic Sankey Diagram
4545

4646
```python
47-
import plotly.plotly as py
47+
import plotly.graph_objs as go
4848

49-
data = dict(
50-
type='sankey',
49+
data = go.Sankey(
5150
node = dict(
5251
pad = 15,
5352
thickness = 20,
54-
line = dict(
55-
color = "black",
56-
width = 0.5
57-
),
53+
line = dict(color = "black", width = 0.5),
5854
label = ["A1", "A2", "B1", "B2", "C1", "C2"],
59-
color = ["blue", "blue", "blue", "blue", "blue", "blue"]
55+
color = "blue"
6056
),
6157
link = dict(
62-
source = [0,1,0,2,3,3],
63-
target = [2,3,3,4,4,5],
64-
value = [8,4,2,8,4,2]
58+
source = [0, 1, 0, 2, 3, 3], # indices correspond to labels, eg A1, A2, A2, B1, ...
59+
target = [2, 3, 3, 4, 4, 5],
60+
value = [8, 4, 2, 8, 4, 2]
6561
))
6662

67-
layout = dict(
68-
title = "Basic Sankey Diagram",
69-
font = dict(
70-
size = 10
71-
)
72-
)
73-
74-
fig = dict(data=[data], layout=layout)
75-
py.iplot(fig, validate=False)
76-
```
77-
78-
### Create Sankey Canvas
79-
80-
```python inputHidden=false outputHidden=false
81-
import plotly.plotly as py
8263

83-
data = dict(
84-
type='sankey',
85-
domain = dict(
86-
x = [0,1],
87-
y = [0,1]
88-
),
89-
orientation = "h",
90-
valueformat = ".0f",
91-
valuesuffix = "TWh"
92-
)
93-
94-
layout = dict(
95-
title = "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>",
96-
font = dict(
97-
size = 10
98-
)
99-
)
64+
fig = go.Figure(data=[data])
65+
fig.update(layout_title_text="Basic Sankey Diagram", layout_font_size=10)
66+
fig.show()
10067
```
10168

102-
### Add Nodes
69+
### More complex Sankey diagram
10370

10471
```python inputHidden=false outputHidden=false
105-
import plotly.plotly as py
106-
10772
import urllib, json
10873

10974
url = 'https://raw.githubusercontent.com/plotly/plotly.js/master/test/image/mocks/sankey_energy.json'
110-
response = urllib.urlopen(url)
75+
response = urllib.request.urlopen(url)
11176
data = json.loads(response.read())
11277

113-
data_trace = dict(
114-
type='sankey',
115-
domain = dict(
116-
x = [0,1],
117-
y = [0,1]
118-
),
119-
orientation = "h",
78+
data_trace = go.Sankey(
12079
valueformat = ".0f",
12180
valuesuffix = "TWh",
81+
# Define nodes
12282
node = dict(
12383
pad = 15,
12484
thickness = 15,
125-
line = dict(
126-
color = "black",
127-
width = 0.5
128-
),
129-
label = data['data'][0]['node']['label'],
130-
color = data['data'][0]['node']['color']
131-
)
132-
)
133-
134-
layout = dict(
135-
title = "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>",
136-
font = dict(
137-
size = 10
138-
)
139-
)
140-
```
141-
142-
### Add Links
143-
144-
```python inputHidden=false outputHidden=false
145-
import plotly.plotly as py
146-
147-
import urllib, json
148-
149-
url = 'https://raw.githubusercontent.com/plotly/plotly.js/master/test/image/mocks/sankey_energy.json'
150-
response = urllib.urlopen(url)
151-
data = json.loads(response.read())
152-
153-
data_trace = dict(
154-
type='sankey',
155-
width = 1118,
156-
height = 772,
157-
domain = dict(
158-
x = [0,1],
159-
y = [0,1]
160-
),
161-
orientation = "h",
162-
valueformat = ".0f",
163-
valuesuffix = "TWh",
164-
node = dict(
165-
pad = 15,
166-
thickness = 15,
167-
line = dict(
168-
color = "black",
169-
width = 0.5
170-
),
85+
line = dict(color = "black", width = 0.5),
17186
label = data['data'][0]['node']['label'],
17287
color = data['data'][0]['node']['color']
17388
),
89+
# Add links
17490
link = dict(
17591
source = data['data'][0]['link']['source'],
17692
target = data['data'][0]['link']['target'],
17793
value = data['data'][0]['link']['value'],
17894
label = data['data'][0]['link']['label']
17995
))
18096

181-
layout = dict(
182-
title = "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>",
183-
font = dict(
184-
size = 10
185-
)
186-
)
187-
188-
fig = dict(data=[data_trace], layout=layout)
189-
py.iplot(fig, validate=False)
97+
fig = go.Figure(data=[data_trace])
98+
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>",
99+
layout_font_size=10)
100+
fig.show()
190101
```
191102

192103
### Style Sankey Diagram
193104

194-
```python inputHidden=false outputHidden=false
195-
import plotly.plotly as py
196-
105+
```python
197106
import urllib, json
198107

199-
url = 'https://raw.githubusercontent.com/plotly/plotly.js/master/test/image/mocks/sankey_energy_dark.json'
200-
response = urllib.urlopen(url)
108+
url = 'https://raw.githubusercontent.com/plotly/plotly.js/master/test/image/mocks/sankey_energy.json'
109+
response = urllib.request.urlopen(url)
201110
data = json.loads(response.read())
202111

203-
data_trace = dict(
204-
type='sankey',
205-
width = 1118,
206-
height = 772,
207-
domain = dict(
208-
x = [0,1],
209-
y = [0,1]
210-
),
211-
orientation = "h",
112+
data_trace = go.Sankey(
212113
valueformat = ".0f",
213114
valuesuffix = "TWh",
214115
node = dict(
215116
pad = 15,
216117
thickness = 15,
217-
line = dict(
218-
color = "black",
219-
width = 0.5
220-
),
221-
label = data['data'][0]['node']['label']
118+
line = dict(color = "black", width = 0.5),
119+
label = data['data'][0]['node']['label'],
120+
color = data['data'][0]['node']['color']
222121
),
223122
link = dict(
224123
source = data['data'][0]['link']['source'],
@@ -229,40 +128,15 @@ data_trace = dict(
229128

230129
layout = dict(
231130
title = "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>",
232-
font = dict(
233-
size = 10,
234-
color = 'white'
235-
),
131+
font = dict(size = 10, color = 'white'),
236132
plot_bgcolor = 'black',
237133
paper_bgcolor = 'black'
238134
)
239-
240-
fig = dict(data=[data_trace], layout=layout)
241-
py.iplot(fig, validate = False)
135+
fig = go.Figure(data=[data_trace], layout=layout)
136+
fig.show()
242137
```
243138

244139
### Reference
245140

246141
See [https://plot.ly/python/reference/#sankey](https://plot.ly/python/reference/#sankey) for more information and options!
247142

248-
```python
249-
from IPython.display import display, HTML
250-
251-
display(HTML('<link href="//fonts.googleapis.com/css?family=Open+Sans:600,400,300,200|Inconsolata|Ubuntu+Mono:400,700" rel="stylesheet" type="text/css" />'))
252-
display(HTML('<link rel="stylesheet" type="text/css" href="http://help.plot.ly/documentation/all_static/css/ipython-notebook-custom.css">'))
253-
254-
! pip install git+https://github.com/plotly/publisher.git --upgrade
255-
import publisher
256-
publisher.publish(
257-
'sankey.ipynb', 'python/sankey-diagram/', 'Sankey Diagram',
258-
'How to make Sankey Diagrams in Python with Plotly.',
259-
title = 'Sankey Diagram | Plotly',
260-
has_thumbnail='true', thumbnail='thumbnail/sankey.jpg',
261-
language='python',
262-
display_as='basic', order=11,
263-
ipynb= '~notebook_demo/151')
264-
```
265-
266-
```python
267-
268-
```

0 commit comments

Comments
 (0)