1
1
---
2
2
jupyter :
3
3
jupytext :
4
+ notebook_metadata_filter : all
4
5
text_representation :
5
6
extension : .md
6
7
format_name : markdown
7
8
format_version : ' 1.1'
8
9
jupytext_version : 1.1.1
9
10
kernelspec :
10
- display_name : Python 2
11
+ display_name : Python 3
11
12
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
13
24
plotly :
14
25
description : How to make filled area plots in Python with Plotly.
15
26
display_as : basic
@@ -25,260 +36,169 @@ jupyter:
25
36
title : Filled Area Plots | plotly
26
37
---
27
38
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.
34
40
35
- ``` python
36
- import plotly
37
- plotly.__version__
38
- ```
39
41
40
42
#### Basic Overlaid Area Chart
41
43
42
44
``` python
43
- import plotly.plotly as py
44
45
import plotly.graph_objs as go
45
46
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
56
49
57
- data = [trace1, trace2]
58
- py.iplot(data, filename = ' basic-area ' )
50
+ fig = go.Figure( data = [trace0, trace1])
51
+ fig.show( )
59
52
```
60
53
61
54
#### Overlaid Area Chart Without Boundary Lines
62
55
63
56
``` python
64
- import plotly.plotly as py
65
57
import plotly.graph_objs as go
66
58
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' )
79
64
80
- data = [trace1, trace2]
81
- py.iplot(data, filename = ' basic-area-no-bound ' )
65
+ fig = go.Figure( data = [trace0, trace1])
66
+ fig.show( )
82
67
```
83
68
84
69
#### Interior Filling for Area Chart
85
70
86
71
``` python
87
- import plotly.plotly as py
88
72
import plotly.graph_objs as go
89
73
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 ,
94
76
mode = ' lines' ,
95
- line = dict (
96
- color = ' rgb(143, 19, 131)' ,
77
+ line_color = ' indigo' ,
97
78
)
98
- )
99
79
trace1 = go.Scatter(
100
80
x = [1 , 2 , 3 , 4 ],
101
81
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' )
108
84
109
- data = [trace0, trace1]
110
- py.iplot(data, filename = ' filling-interior-area ' )
85
+ fig = go.Figure( data = [trace0, trace1])
86
+ fig.show( )
111
87
```
112
88
113
89
#### Stacked Area Chart
114
90
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
+
115
93
``` python
116
- import plotly.plotly as py
117
94
import plotly.graph_objs as go
118
95
119
- # Add original data
120
96
x= [' Winter' , ' Spring' , ' Summer' , ' Fall' ]
121
97
122
- trace0 = dict (
123
- x = x,
124
- y = [40 , 60 , 40 , 10 ],
98
+ trace0 = go.Scatter(
99
+ x = x, y = [40 , 60 , 40 , 10 ],
125
100
hoverinfo = ' x+y' ,
126
101
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
130
104
)
131
- trace1 = dict (
132
- x = x,
133
- y = [20 , 10 , 10 , 60 ],
105
+ trace1 = go.Scatter(
106
+ x = x, y = [20 , 10 , 10 , 60 ],
134
107
hoverinfo = ' x+y' ,
135
108
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)' ),
138
110
stackgroup = ' one'
139
111
)
140
- trace2 = dict (
141
- x = x,
142
- y = [40 , 30 , 50 , 30 ],
112
+ trace2 = go.Scatter(
113
+ x = x, y = [40 , 30 , 50 , 30 ],
143
114
hoverinfo = ' x+y' ,
144
115
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)' ),
147
117
stackgroup = ' one'
148
118
)
149
- data = [trace0, trace1, trace2]
150
119
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()
153
123
```
154
124
155
125
### Stacked Area Chart with Normalized Values
156
126
157
127
``` python
158
- import plotly.plotly as py
159
128
import plotly.graph_objs as go
160
129
130
+ x= [' Winter' , ' Spring' , ' Summer' , ' Fall' ]
161
131
trace0 = dict (
162
- x = [' Winter' , ' Spring' , ' Summer' , ' Fall' ],
163
- y = [' 40' , ' 20' , ' 30' , ' 40' ],
132
+ x = x, y = [40 , 20 , 30 , 40 ],
164
133
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)' ),
167
135
stackgroup = ' one' ,
168
- groupnorm = ' percent'
136
+ groupnorm = ' percent' # sets the normalization for the sum of the stackgroup
169
137
)
170
138
trace1 = dict (
171
- x = [' Winter' , ' Spring' , ' Summer' , ' Fall' ],
172
- y = [' 50' , ' 70' , ' 40' , ' 60' ],
139
+ x = x, y = [50 , 70 , 40 , 60 ],
173
140
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)' ),
176
142
stackgroup = ' one'
177
143
)
178
144
trace2 = dict (
179
- x = [' Winter' , ' Spring' , ' Summer' , ' Fall' ],
180
- y = [' 70' , ' 80' , ' 60' , ' 70' ],
145
+ x = x, y = [70 , 80 , 60 , 70 ],
181
146
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)' ),
184
148
stackgroup = ' one'
185
149
)
186
150
trace3 = dict (
187
- x = [' Winter' , ' Spring' , ' Summer' , ' Fall' ],
188
- y = [' 100' , ' 100' , ' 100' , ' 100' ],
151
+ x = x, y = [100 , 100 , 100 , 100 ],
189
152
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)' ),
192
154
stackgroup = ' one'
193
155
)
194
- data = [trace0, trace1, trace2, trace3]
156
+
195
157
layout = go.Layout(
196
158
showlegend = True ,
197
- xaxis = dict (
198
- type = ' category' ,
199
- ),
159
+ xaxis_type = ' category' ,
200
160
yaxis = dict (
201
161
type = ' linear' ,
202
162
range = [1 , 100 ],
203
163
dtick = 20 ,
204
164
ticksuffix = ' %'
205
165
)
206
166
)
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( )
209
169
```
210
170
211
171
#### Select Hover Points
212
172
213
173
``` python
214
- import plotly.plotly as py
215
174
import plotly.graph_objs as go
216
175
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' )
242
182
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' )
244
189
245
190
layout = go.Layout(
246
191
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 ]
253
194
)
254
195
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( )
257
198
```
258
199
259
200
#### Reference
260
201
See https://plot.ly/python/reference/#scatter-line
261
202
and https://plot.ly/python/reference/#scatter-fill
262
203
for more information and attribute options!
263
204
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