Show Sidebar Hide Sidebar

Error Bars in Python

How to add error-bars to charts in Python with Plotly.

New to Plotly?

Plotly's Python library is free and open source! Get started by downloading the client and reading the primer.
You can set up Plotly to work in online or offline mode, or in jupyter notebooks.
We also have a quick-reference cheatsheet (new!) to help you get started!

Basic Symmetric Error Bars

In [1]:
import plotly.plotly as py
import plotly.graph_objs as go

data = [
    go.Scatter(
        x=[0, 1, 2],
        y=[6, 10, 2],
        error_y=dict(
            type='data',
            array=[1, 2, 3],
            visible=True
        )
    )
]

py.iplot(data, filename='basic-error-bar')
Out[1]:

Asymmetric Error Bars

In [2]:
import plotly.plotly as py
import plotly.graph_objs as go

data = [
    go.Scatter(
        x=[1, 2, 3, 4],
        y=[2, 1, 3, 4],
        error_y=dict(
            type='data',
            symmetric=False,
            array=[0.1, 0.2, 0.1, 0.1],
            arrayminus=[0.2, 0.4, 1, 0.2]
        )
    )
]
py.iplot(data, filename='error-bar-asymmetric-array')
Out[2]:

Error Bars as a Percentage of the y Value

In [3]:
import plotly.plotly as py
import plotly.graph_objs as go

data = [
    go.Scatter(
        x=[0, 1, 2],
        y=[6, 10, 2],
        error_y=dict(
            type='percent',
            value=50,
            visible=True
        )
    )
]
py.iplot(data, filename='percent-error-bar')
Out[3]:

Asymmetric Error Bars with a Constant Offset

In [4]:
import plotly.plotly as py
import plotly.graph_objs as go

data = [
    go.Scatter(
        x=[1, 2, 3, 4],
        y=[2, 1, 3, 4],
        error_y=dict(
            type='percent',
            symmetric=False,
            value=15,
            valueminus=25
        )
    )
]
py.iplot(data, filename='error-bar-asymmetric-constant')
Out[4]:

Horizontal Error Bars

In [5]:
import plotly.plotly as py
import plotly.graph_objs as go

data = [
    go.Scatter(
        x=[1, 2, 3, 4],
        y=[2, 1, 3, 4],
        error_x=dict(
            type='percent',
            value=10
        )
    )
]
py.iplot(data, filename='error-bar-horizontal')
Out[5]:

Bar Chart with Error Bars

In [ ]:
import plotly.plotly as py
import plotly.graph_objs as go

trace1 = go.Bar(
    x=['Trial 1', 'Trial 2', 'Trial 3'],
    y=[3, 6, 4],
    name='Control',
    error_y=dict(
        type='data',
        array=[1, 0.5, 1.5],
        visible=True
    )
)
trace2 = go.Bar(
    x=['Trial 1', 'Trial 2', 'Trial 3'],
    y=[4, 7, 3],
    name='Experimental',
    error_y=dict(
        type='data',
        array=[0.5, 1, 2],
        visible=True
    )
)
data = [trace1, trace2]
layout = go.Layout(
    barmode='group'
)
fig = go.Figure(data=data, layout=layout)
py.iplot(fig, filename='error-bar-bar')
Out[ ]:

Colored and Styled Error Bars

In [ ]:
import plotly.plotly as py
import plotly.graph_objs as go

import numpy as np

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

trace1 = go.Scatter(
    x=x_theo,
    y=sincx,
    name='sinc(x)'
)
trace2 = go.Scatter(
    x=x,
    y=y,
    mode='markers',
    name='measured',
    error_y=dict(
        type='constant',
        value=0.1,
        color='#85144B',
        thickness=1.5,
        width=3,
        opacity=1
    ),
    error_x=dict(
        type='constant',
        value=0.2,
        color='#85144B',
        thickness=1.5,
        width=3,
        opacity=1
    ),
    marker=dict(
        color='#85144B',
        size=8
    )
)
data = [trace1, trace2]
py.iplot(data, filename='error-bar-style')
Still need help?
Contact Us

For guaranteed 24 hour response turnarounds, upgrade to a Developer Support Plan.