Skip to content

Commit a2a6c77

Browse files
authored
Merge pull request #37 from plotly/errorbar
error bars notebook
2 parents 47c561c + 6a6c339 commit a2a6c77

File tree

1 file changed

+89
-109
lines changed

1 file changed

+89
-109
lines changed

notebooks/error-bars.md

Lines changed: 89 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,19 @@ jupyter:
88
format_version: '1.1'
99
jupytext_version: 1.1.1
1010
kernelspec:
11-
display_name: Python 2
11+
display_name: Python 3
1212
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
1424
plotly:
1525
description: How to add error-bars to charts in Python with Plotly.
1626
display_as: statistical
@@ -24,217 +34,187 @@ jupyter:
2434
permalink: python/error-bars/
2535
thumbnail: thumbnail/error-bar.jpg
2636
title: Error Bars | plotly
37+
v4upgrade: true
2738
---
2839

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
3341

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
3466

3567
#### Basic Symmetric Error Bars
3668

3769
```python
38-
import plotly.plotly as py
39-
import plotly.graph_objs as go
70+
import plotly.graph_objects as go
4071

41-
data = [
42-
go.Scatter(
72+
fig = go.Figure(data=go.Scatter(
4373
x=[0, 1, 2],
4474
y=[6, 10, 2],
4575
error_y=dict(
46-
type='data',
76+
type='data', # value of error bar given in data coordinates
4777
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()
5481
```
5582

5683
#### Asymmetric Error Bars
5784

5885
```python
59-
import plotly.plotly as py
60-
import plotly.graph_objs as go
86+
import plotly.graph_objects as go
6187

62-
data = [
63-
go.Scatter(
88+
fig = go.Figure(data=go.Scatter(
6489
x=[1, 2, 3, 4],
6590
y=[2, 1, 3, 4],
6691
error_y=dict(
6792
type='data',
6893
symmetric=False,
6994
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()
7598
```
7699

77100
#### Error Bars as a Percentage of the y Value
78101

79102
```python
80-
import plotly.plotly as py
81-
import plotly.graph_objs as go
103+
import plotly.graph_objects as go
82104

83-
data = [
84-
go.Scatter(
105+
fig = go.Figure(data=go.Scatter(
85106
x=[0, 1, 2],
86107
y=[6, 10, 2],
87108
error_y=dict(
88-
type='percent',
109+
type='percent', # value of error bar given as percentage of y value
89110
value=50,
90-
visible=True
91-
)
92-
)
93-
]
94-
py.iplot(data, filename='percent-error-bar')
111+
visible=True)
112+
))
113+
fig.show()
95114
```
96115

97116
#### Asymmetric Error Bars with a Constant Offset
98117

99118
```python
100-
import plotly.plotly as py
101-
import plotly.graph_objs as go
119+
import plotly.graph_objects as go
102120

103-
data = [
104-
go.Scatter(
121+
fig = go.Figure(data=go.Scatter(
105122
x=[1, 2, 3, 4],
106123
y=[2, 1, 3, 4],
107124
error_y=dict(
108125
type='percent',
109126
symmetric=False,
110127
value=15,
111-
valueminus=25
112-
)
113-
)
114-
]
115-
py.iplot(data, filename='error-bar-asymmetric-constant')
128+
valueminus=25)
129+
))
130+
fig.show()
116131
```
117132

118133
#### Horizontal Error Bars
119134

120135
```python
121-
import plotly.plotly as py
122-
import plotly.graph_objs as go
136+
import plotly.graph_objects as go
123137

124-
data = [
125-
go.Scatter(
138+
fig = go.Figure(data=go.Scatter(
126139
x=[1, 2, 3, 4],
127140
y=[2, 1, 3, 4],
128141
error_x=dict(
129142
type='percent',
130-
value=10
131-
)
132-
)
133-
]
134-
py.iplot(data, filename='error-bar-horizontal')
143+
value=10)
144+
))
145+
fig.show()
135146
```
136147

137148
#### Bar Chart with Error Bars
138149

139150
```python
140-
import plotly.plotly as py
141-
import plotly.graph_objs as go
151+
import plotly.graph_objects as go
142152

143-
trace1 = go.Bar(
153+
fig = go.Figure()
154+
fig.add_trace(go.Bar(
144155
x=['Trial 1', 'Trial 2', 'Trial 3'],
145156
y=[3, 6, 4],
146157
name='Control',
147158
error_y=dict(
148159
type='data',
149160
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(
154165
x=['Trial 1', 'Trial 2', 'Trial 3'],
155166
y=[4, 7, 3],
156167
name='Experimental',
157168
error_y=dict(
158169
type='data',
159170
array=[0.5, 1, 2],
160-
visible=True
171+
visible=True,
172+
color='white'
161173
)
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()
169177
```
170178

171179
#### Colored and Styled Error Bars
172180

173181
```python
174-
import plotly.plotly as py
175-
import plotly.graph_objs as go
176-
182+
import plotly.graph_objects as go
177183
import numpy as np
178184

179185
x_theo = np.linspace(-4, 4, 100)
180186
sincx = np.sinc(x_theo)
181187
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]
182188
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]
183189

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,
187193
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,
192197
mode='markers',
193198
name='measured',
194199
error_y=dict(
195200
type='constant',
196201
value=0.1,
197-
color='#85144B',
202+
color='purple',
198203
thickness=1.5,
199204
width=3,
200205
),
201206
error_x=dict(
202207
type='constant',
203208
value=0.2,
204-
color='#85144B',
209+
color='purple',
205210
thickness=1.5,
206211
width=3,
207212
),
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()
215216
```
216217

217218
#### Reference
218219
See https://plot.ly/python/reference/#scatter for more information and chart attribute options!
219220

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

Comments
 (0)