@@ -8,9 +8,19 @@ jupyter:
8
8
format_version : ' 1.1'
9
9
jupytext_version : 1.1.1
10
10
kernelspec :
11
- display_name : Python 2
11
+ display_name : Python 3
12
12
language : python
13
- 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
14
24
plotly :
15
25
description : How to add error-bars to charts in Python with Plotly.
16
26
display_as : statistical
@@ -24,217 +34,187 @@ jupyter:
24
34
permalink : python/error-bars/
25
35
thumbnail : thumbnail/error-bar.jpg
26
36
title : Error Bars | plotly
37
+ v4upgrade : true
27
38
---
28
39
29
- #### New to Plotly?
30
- 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/ ) .
31
- <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 ) .
32
- <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!
40
+ ### Error Bars with plotly express
33
41
42
+ Plotly express functions take as argument a tidy [ pandas DataFrame] ( https://pandas.pydata.org/pandas-docs/stable/getting_started/10min.html ) . For functions representing 2D data points such as [ ` px.scatter ` ] ( https://plot.ly/python/line-and-scatter/ ) , [ ` px.line ` ] ( https://plot.ly/python/line-charts/ ) , [ ` px.bar ` ] ( https://plot.ly/python/bar-charts/ ) etc., error bars are given as a column name which is the value of the ` error_x ` (for the error on x position) and ` error_y ` (for the error on y position).
43
+
44
+ ``` python
45
+ import plotly.express as px
46
+ iris = px.data.iris()
47
+ iris[" e" ] = iris[" sepal_width" ]/ 100
48
+ fig = px.scatter(iris, x = " sepal_width" , y = " sepal_length" , color = " species" ,
49
+ error_x = " e" , error_y = " e" )
50
+ fig.show()
51
+ ```
52
+
53
+ #### Asymmetric Error Bars with plotly express
54
+
55
+ ``` python
56
+ import plotly.express as px
57
+ iris = px.data.iris()
58
+ iris[" e_plus" ] = iris[" sepal_width" ]/ 100
59
+ iris[" e_minus" ] = iris[" sepal_width" ]/ 40
60
+ fig = px.scatter(iris, x = " sepal_width" , y = " sepal_length" , color = " species" ,
61
+ error_y = " e_plus" , error_y_minus = " e_minus" )
62
+ fig.show()
63
+ ```
64
+
65
+ ### Error Bars with graph_objects
34
66
35
67
#### Basic Symmetric Error Bars
36
68
37
69
``` python
38
- import plotly.plotly as py
39
- import plotly.graph_objs as go
70
+ import plotly.graph_objects as go
40
71
41
- data = [
42
- go.Scatter(
72
+ fig = go.Figure(data = go.Scatter(
43
73
x = [0 , 1 , 2 ],
44
74
y = [6 , 10 , 2 ],
45
75
error_y = dict (
46
- type = ' data' ,
76
+ type = ' data' , # value of error bar given in data coordinates
47
77
array = [1 , 2 , 3 ],
48
- visible = True
49
- )
50
- )
51
- ]
52
-
53
- py.iplot(data, filename = ' basic-error-bar' )
78
+ visible = True )
79
+ ))
80
+ fig.show()
54
81
```
55
82
56
83
#### Asymmetric Error Bars
57
84
58
85
``` python
59
- import plotly.plotly as py
60
- import plotly.graph_objs as go
86
+ import plotly.graph_objects as go
61
87
62
- data = [
63
- go.Scatter(
88
+ fig = go.Figure(data = go.Scatter(
64
89
x = [1 , 2 , 3 , 4 ],
65
90
y = [2 , 1 , 3 , 4 ],
66
91
error_y = dict (
67
92
type = ' data' ,
68
93
symmetric = False ,
69
94
array = [0.1 , 0.2 , 0.1 , 0.1 ],
70
- arrayminus = [0.2 , 0.4 , 1 , 0.2 ]
71
- )
72
- )
73
- ]
74
- py.iplot(data, filename = ' error-bar-asymmetric-array' )
95
+ arrayminus = [0.2 , 0.4 , 1 , 0.2 ])
96
+ ))
97
+ fig.show()
75
98
```
76
99
77
100
#### Error Bars as a Percentage of the y Value
78
101
79
102
``` python
80
- import plotly.plotly as py
81
- import plotly.graph_objs as go
103
+ import plotly.graph_objects as go
82
104
83
- data = [
84
- go.Scatter(
105
+ fig = go.Figure(data = go.Scatter(
85
106
x = [0 , 1 , 2 ],
86
107
y = [6 , 10 , 2 ],
87
108
error_y = dict (
88
- type = ' percent' ,
109
+ type = ' percent' , # value of error bar given as percentage of y value
89
110
value = 50 ,
90
- visible = True
91
- )
92
- )
93
- ]
94
- py.iplot(data, filename = ' percent-error-bar' )
111
+ visible = True )
112
+ ))
113
+ fig.show()
95
114
```
96
115
97
116
#### Asymmetric Error Bars with a Constant Offset
98
117
99
118
``` python
100
- import plotly.plotly as py
101
- import plotly.graph_objs as go
119
+ import plotly.graph_objects as go
102
120
103
- data = [
104
- go.Scatter(
121
+ fig = go.Figure(data = go.Scatter(
105
122
x = [1 , 2 , 3 , 4 ],
106
123
y = [2 , 1 , 3 , 4 ],
107
124
error_y = dict (
108
125
type = ' percent' ,
109
126
symmetric = False ,
110
127
value = 15 ,
111
- valueminus = 25
112
- )
113
- )
114
- ]
115
- py.iplot(data, filename = ' error-bar-asymmetric-constant' )
128
+ valueminus = 25 )
129
+ ))
130
+ fig.show()
116
131
```
117
132
118
133
#### Horizontal Error Bars
119
134
120
135
``` python
121
- import plotly.plotly as py
122
- import plotly.graph_objs as go
136
+ import plotly.graph_objects as go
123
137
124
- data = [
125
- go.Scatter(
138
+ fig = go.Figure(data = go.Scatter(
126
139
x = [1 , 2 , 3 , 4 ],
127
140
y = [2 , 1 , 3 , 4 ],
128
141
error_x = dict (
129
142
type = ' percent' ,
130
- value = 10
131
- )
132
- )
133
- ]
134
- py.iplot(data, filename = ' error-bar-horizontal' )
143
+ value = 10 )
144
+ ))
145
+ fig.show()
135
146
```
136
147
137
148
#### Bar Chart with Error Bars
138
149
139
150
``` python
140
- import plotly.plotly as py
141
- import plotly.graph_objs as go
151
+ import plotly.graph_objects as go
142
152
143
- trace1 = go.Bar(
153
+ fig = go.Figure()
154
+ fig.add_trace(go.Bar(
144
155
x = [' Trial 1' , ' Trial 2' , ' Trial 3' ],
145
156
y = [3 , 6 , 4 ],
146
157
name = ' Control' ,
147
158
error_y = dict (
148
159
type = ' data' ,
149
160
array = [1 , 0.5 , 1.5 ],
150
- visible = True
151
- )
152
- )
153
- trace2 = go.Bar(
161
+ visible = True ,
162
+ color = ' white ' )
163
+ ))
164
+ fig.add_trace( go.Bar(
154
165
x = [' Trial 1' , ' Trial 2' , ' Trial 3' ],
155
166
y = [4 , 7 , 3 ],
156
167
name = ' Experimental' ,
157
168
error_y = dict (
158
169
type = ' data' ,
159
170
array = [0.5 , 1 , 2 ],
160
- visible = True
171
+ visible = True ,
172
+ color = ' white'
161
173
)
162
- )
163
- data = [trace1, trace2]
164
- layout = go.Layout(
165
- barmode = ' group'
166
- )
167
- fig = go.Figure(data = data, layout = layout)
168
- py.iplot(fig, filename = ' error-bar-bar' )
174
+ ))
175
+ fig.update_layout(barmode = ' group' )
176
+ fig.show()
169
177
```
170
178
171
179
#### Colored and Styled Error Bars
172
180
173
181
``` python
174
- import plotly.plotly as py
175
- import plotly.graph_objs as go
176
-
182
+ import plotly.graph_objects as go
177
183
import numpy as np
178
184
179
185
x_theo = np.linspace(- 4 , 4 , 100 )
180
186
sincx = np.sinc(x_theo)
181
187
x = [- 3.8 , - 3.03 , - 1.91 , - 1.46 , - 0.89 , - 0.24 , - 0.0 , 0.41 , 0.89 , 1.01 , 1.91 , 2.28 , 2.79 , 3.56 ]
182
188
y = [- 0.02 , 0.04 , - 0.01 , - 0.27 , 0.36 , 0.75 , 1.03 , 0.65 , 0.28 , 0.02 , - 0.11 , 0.16 , 0.04 , - 0.15 ]
183
189
184
- trace1 = go.Scatter(
185
- x = x_theo,
186
- y = sincx,
190
+ fig = go.Figure()
191
+ fig.add_trace(go.Scatter(
192
+ x = x_theo, y = sincx,
187
193
name = ' sinc(x)'
188
- )
189
- trace2 = go.Scatter(
190
- x = x,
191
- y = y,
194
+ ))
195
+ fig.add_trace(go.Scatter(
196
+ x = x, y = y,
192
197
mode = ' markers' ,
193
198
name = ' measured' ,
194
199
error_y = dict (
195
200
type = ' constant' ,
196
201
value = 0.1 ,
197
- color = ' #85144B ' ,
202
+ color = ' purple ' ,
198
203
thickness = 1.5 ,
199
204
width = 3 ,
200
205
),
201
206
error_x = dict (
202
207
type = ' constant' ,
203
208
value = 0.2 ,
204
- color = ' #85144B ' ,
209
+ color = ' purple ' ,
205
210
thickness = 1.5 ,
206
211
width = 3 ,
207
212
),
208
- marker = dict (
209
- color = ' #85144B' ,
210
- size = 8
211
- )
212
- )
213
- data = [trace1, trace2]
214
- py.iplot(data, filename = ' error-bar-style' )
213
+ marker = dict (color = ' purple' , size = 8 )
214
+ ))
215
+ fig.show()
215
216
```
216
217
217
218
#### Reference
218
219
See https://plot.ly/python/reference/#scatter for more information and chart attribute options!
219
220
220
- ``` python
221
- from IPython.display import display, HTML
222
-
223
- 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" />' ))
224
- display(HTML(' <link rel="stylesheet" type="text/css" href="http://help.plot.ly/documentation/all_static/css/ipython-notebook-custom.css">' ))
225
-
226
- ! pip install git+ https:// github.com/ plotly/ publisher.git -- upgrade
227
- import publisher
228
- publisher.publish(
229
- ' error-bars.ipynb' , ' python/error-bars/' , ' Error Bars | plotly' ,
230
- ' How to add error-bars to charts in Python with Plotly.' ,
231
- title = ' Error Bars | plotly' ,
232
- name = ' Error Bars' ,
233
- thumbnail = ' thumbnail/error-bar.jpg' , language = ' python' ,
234
- page_type = ' example_index' , has_thumbnail = ' true' , display_as = ' statistical' , order = 1 ,
235
- ipynb = ' ~notebook_demo/18' )
236
- ```
237
-
238
- ``` python
239
-
240
- ```
0 commit comments