Skip to content

Commit 295631e

Browse files
committed
filled-area-plots notebook
1 parent 991a6b2 commit 295631e

File tree

1 file changed

+77
-157
lines changed

1 file changed

+77
-157
lines changed

notebooks/filled-area-plots.md

Lines changed: 77 additions & 157 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,26 @@
11
---
22
jupyter:
33
jupytext:
4+
notebook_metadata_filter: all
45
text_representation:
56
extension: .md
67
format_name: markdown
78
format_version: '1.1'
89
jupytext_version: 1.1.1
910
kernelspec:
10-
display_name: Python 2
11+
display_name: Python 3
1112
language: python
12-
name: python2
13+
name: python3
14+
language_info:
15+
codemirror_mode:
16+
name: ipython
17+
version: 3
18+
file_extension: .py
19+
mimetype: text/x-python
20+
name: python
21+
nbconvert_exporter: python
22+
pygments_lexer: ipython3
23+
version: 3.6.7
1324
plotly:
1425
description: How to make filled area plots in Python with Plotly.
1526
display_as: basic
@@ -25,260 +36,169 @@ jupyter:
2536
title: Filled Area Plots | plotly
2637
---
2738

28-
#### New to Plotly?
29-
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/).
30-
<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).
31-
<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!
32-
#### Version Check
33-
Plotly's python package is updated frequently. Run `pip install plotly --upgrade` to use the latest version.
39+
This example shows how to fill the area enclosed by traces.
3440

35-
```python
36-
import plotly
37-
plotly.__version__
38-
```
3941

4042
#### Basic Overlaid Area Chart
4143

4244
```python
43-
import plotly.plotly as py
4445
import plotly.graph_objs as go
4546

46-
trace1 = go.Scatter(
47-
x=[1, 2, 3, 4],
48-
y=[0, 2, 3, 5],
49-
fill='tozeroy'
50-
)
51-
trace2 = go.Scatter(
52-
x=[1, 2, 3, 4],
53-
y=[3, 5, 1, 7],
54-
fill='tonexty'
55-
)
47+
trace0 = go.Scatter(x=[1, 2, 3, 4], y=[0, 2, 3, 5], fill='tozeroy') # fill down to xaxis
48+
trace1 = go.Scatter(x=[1, 2, 3, 4], y=[3, 5, 1, 7], fill='tonexty') # fill to trace0 y
5649

57-
data = [trace1, trace2]
58-
py.iplot(data, filename='basic-area')
50+
fig = go.Figure(data=[trace0, trace1])
51+
fig.show()
5952
```
6053

6154
#### Overlaid Area Chart Without Boundary Lines
6255

6356
```python
64-
import plotly.plotly as py
6557
import plotly.graph_objs as go
6658

67-
trace1 = go.Scatter(
68-
x=[1, 2, 3, 4],
69-
y=[0, 2, 3, 5],
70-
fill='tozeroy',
71-
mode= 'none'
72-
)
73-
trace2 = go.Scatter(
74-
x=[1, 2, 3, 4],
75-
y=[3, 5, 1, 7],
76-
fill='tonexty',
77-
mode= 'none'
78-
)
59+
trace0 = go.Scatter(x=[1, 2, 3, 4], y=[0, 2, 3, 5], fill='tozeroy',
60+
mode='none' # override default markers+lines
61+
)
62+
trace1 = go.Scatter(x=[1, 2, 3, 4], y=[3, 5, 1, 7], fill='tonexty',
63+
mode= 'none')
7964

80-
data = [trace1, trace2]
81-
py.iplot(data, filename='basic-area-no-bound')
65+
fig = go.Figure(data=[trace0, trace1])
66+
fig.show()
8267
```
8368

8469
#### Interior Filling for Area Chart
8570

8671
```python
87-
import plotly.plotly as py
8872
import plotly.graph_objs as go
8973

90-
trace0 = go.Scatter(
91-
x=[1, 2, 3, 4],
92-
y=[3, 4, 8, 3],
93-
fill= None,
74+
trace0 = go.Scatter(x=[1, 2, 3, 4], y=[3, 4, 8, 3],
75+
fill=None,
9476
mode='lines',
95-
line=dict(
96-
color='rgb(143, 19, 131)',
77+
line_color='indigo',
9778
)
98-
)
9979
trace1 = go.Scatter(
10080
x=[1, 2, 3, 4],
10181
y=[1, 6, 2, 6],
102-
fill='tonexty',
103-
mode='lines',
104-
line=dict(
105-
color='rgb(143, 19, 131)',
106-
)
107-
)
82+
fill='tonexty', # fill area between trace0 and trace1
83+
mode='lines', line_color='indigo')
10884

109-
data = [trace0, trace1]
110-
py.iplot(data, filename='filling-interior-area')
85+
fig = go.Figure(data=[trace0, trace1])
86+
fig.show()
11187
```
11288

11389
#### Stacked Area Chart
11490

91+
The `stackgroup` parameter is used to add the `y` values of the different traces in the same group. Traces in the same group fill up to the next trace of the group.
92+
11593
```python
116-
import plotly.plotly as py
11794
import plotly.graph_objs as go
11895

119-
# Add original data
12096
x=['Winter', 'Spring', 'Summer', 'Fall']
12197

122-
trace0 = dict(
123-
x=x,
124-
y=[40, 60, 40, 10],
98+
trace0 = go.Scatter(
99+
x=x, y=[40, 60, 40, 10],
125100
hoverinfo='x+y',
126101
mode='lines',
127-
line=dict(width=0.5,
128-
color='rgb(131, 90, 241)'),
129-
stackgroup='one'
102+
line=dict(width=0.5, color='rgb(131, 90, 241)'),
103+
stackgroup='one' # define stack group
130104
)
131-
trace1 = dict(
132-
x=x,
133-
y=[20, 10, 10, 60],
105+
trace1 = go.Scatter(
106+
x=x, y=[20, 10, 10, 60],
134107
hoverinfo='x+y',
135108
mode='lines',
136-
line=dict(width=0.5,
137-
color='rgb(111, 231, 219)'),
109+
line=dict(width=0.5, color='rgb(111, 231, 219)'),
138110
stackgroup='one'
139111
)
140-
trace2 = dict(
141-
x=x,
142-
y=[40, 30, 50, 30],
112+
trace2 = go.Scatter(
113+
x=x, y=[40, 30, 50, 30],
143114
hoverinfo='x+y',
144115
mode='lines',
145-
line=dict(width=0.5,
146-
color='rgb(184, 247, 212)'),
116+
line=dict(width=0.5, color='rgb(184, 247, 212)'),
147117
stackgroup='one'
148118
)
149-
data = [trace0, trace1, trace2]
150119

151-
fig = dict(data=data)
152-
py.iplot(fig, filename='stacked-area-plot-hover', validate=False)
120+
fig = go.Figure(data=[trace0, trace1, trace2])
121+
fig.update(layout_yaxis_range=(0, 100))
122+
fig.show()
153123
```
154124

155125
### Stacked Area Chart with Normalized Values
156126

157127
```python
158-
import plotly.plotly as py
159128
import plotly.graph_objs as go
160129

130+
x=['Winter', 'Spring', 'Summer', 'Fall']
161131
trace0 = dict(
162-
x=['Winter', 'Spring', 'Summer', 'Fall'],
163-
y=['40', '20', '30', '40'],
132+
x=x, y=[40, 20, 30, 40],
164133
mode='lines',
165-
line=dict(width=0.5,
166-
color='rgb(184, 247, 212)'),
134+
line=dict(width=0.5, color='rgb(184, 247, 212)'),
167135
stackgroup='one',
168-
groupnorm='percent'
136+
groupnorm='percent' # sets the normalization for the sum of the stackgroup
169137
)
170138
trace1 = dict(
171-
x=['Winter', 'Spring', 'Summer', 'Fall'],
172-
y=['50', '70', '40', '60'],
139+
x=x, y=[50, 70, 40, 60],
173140
mode='lines',
174-
line=dict(width=0.5,
175-
color='rgb(111, 231, 219)'),
141+
line=dict(width=0.5, color='rgb(111, 231, 219)'),
176142
stackgroup='one'
177143
)
178144
trace2 = dict(
179-
x=['Winter', 'Spring', 'Summer', 'Fall'],
180-
y=['70', '80', '60', '70'],
145+
x=x, y=[70, 80, 60, 70],
181146
mode='lines',
182-
line=dict(width=0.5,
183-
color='rgb(127, 166, 238)'),
147+
line=dict(width=0.5, color='rgb(127, 166, 238)'),
184148
stackgroup='one'
185149
)
186150
trace3 = dict(
187-
x=['Winter', 'Spring', 'Summer', 'Fall'],
188-
y=['100', '100', '100', '100'],
151+
x=x, y=[100, 100, 100, 100],
189152
mode='lines',
190-
line=dict(width=0.5,
191-
color='rgb(131, 90, 241)'),
153+
line=dict(width=0.5, color='rgb(131, 90, 241)'),
192154
stackgroup='one'
193155
)
194-
data = [trace0, trace1, trace2, trace3]
156+
195157
layout = go.Layout(
196158
showlegend=True,
197-
xaxis=dict(
198-
type='category',
199-
),
159+
xaxis_type='category',
200160
yaxis=dict(
201161
type='linear',
202162
range=[1, 100],
203163
dtick=20,
204164
ticksuffix='%'
205165
)
206166
)
207-
fig = dict(data=data, layout=layout)
208-
py.iplot(fig, filename='stacked-area-plot-norm', validate=False)
167+
fig = go.Figure(data=[trace0, trace1, trace2, trace3], layout=layout)
168+
fig.show()
209169
```
210170

211171
#### Select Hover Points
212172

213173
```python
214-
import plotly.plotly as py
215174
import plotly.graph_objs as go
216175

217-
trace0 = go.Scatter(
218-
x=[0,0.5,1,1.5,2],
219-
y=[0,1,2,1,0],
220-
fill= 'toself',
221-
fillcolor = '#ab63fa',
222-
hoveron = 'points+fills',
223-
line = dict(
224-
color = '#ab63fa'
225-
),
226-
text = "Points + Fills",
227-
hoverinfo = 'text'
228-
)
229-
230-
trace1 = go.Scatter(
231-
x=[3,3.5,4,4.5,5],
232-
y=[0,1,2,1,0],
233-
fill='toself',
234-
fillcolor = '#e763fa',
235-
hoveron = 'points',
236-
line = dict(
237-
color = '#e763fa'
238-
),
239-
text = "Points only",
240-
hoverinfo = 'text'
241-
)
176+
trace0 = go.Scatter(x=[0,0.5,1,1.5,2], y=[0,1,2,1,0],
177+
fill='toself', fillcolor='darkviolet',
178+
hoveron = 'points+fills', # select where hover is active
179+
line_color='darkviolet',
180+
text="Points + Fills",
181+
hoverinfo = 'text+x+y')
242182

243-
data = [trace0, trace1]
183+
trace1 = go.Scatter(x=[3,3.5,4,4.5,5], y=[0,1,2,1,0],
184+
fill='toself', fillcolor = 'violet',
185+
hoveron='points',
186+
line_color='violet',
187+
text="Points only",
188+
hoverinfo='text+x+y')
244189

245190
layout = go.Layout(
246191
title = "hover on <i>points</i> or <i>fill</i>",
247-
xaxis = dict(
248-
range = [0,5.2]
249-
),
250-
yaxis = dict(
251-
range = [0,3]
252-
)
192+
xaxis_range = [0,5.2],
193+
yaxis_range = [0,3]
253194
)
254195

255-
fig = go.Figure(data=data,layout=layout)
256-
py.iplot(data, filename='select-hover-points')
196+
fig = go.Figure(data=[trace0, trace1],layout=layout)
197+
fig.show()
257198
```
258199

259200
#### Reference
260201
See https://plot.ly/python/reference/#scatter-line
261202
and https://plot.ly/python/reference/#scatter-fill
262203
for more information and attribute options!
263204

264-
```python
265-
from IPython.display import display, HTML
266-
267-
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" />'))
268-
display(HTML('<link rel="stylesheet" type="text/css" href="http://help.plot.ly/documentation/all_static/css/ipython-notebook-custom.css">'))
269-
270-
! pip install git+https://github.com/plotly/publisher.git --upgrade
271-
import publisher
272-
publisher.publish(
273-
'area.ipynb', 'python/filled-area-plots/', 'Filled Area Plots | plotly',
274-
'How to make filled area plots in Python with Plotly.',
275-
title = 'Filled Area Plots | plotly',
276-
name = 'Filled Area Plots',
277-
thumbnail='thumbnail/area.jpg', language='python',
278-
has_thumbnail='true', display_as='basic', order=3.5,
279-
ipynb='~notebook_demo/8')
280-
```
281-
282-
```python
283-
284-
```

0 commit comments

Comments
 (0)