Show Sidebar Hide Sidebar

Error Bars in Pandas

How to add error bars to a line, scatter, or bar chart. Seven examples of symmetric, asymmetric, horizontal, and colored error bars.

# Learn about API authentication here: https://plot.ly/pandas/getting-started
# Find your api_key here: https://plot.ly/settings/api

import plotly.plotly as py
import plotly.graph_objs as go
import pandas as pd
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/wind_speed_laurel_nebraska.csv')
df.head()

trace = go.Scatter(
    x=df['Time'],
    y=df['10 Min Sampled Avg'],
    mode='lines',
    error_y=dict(
        type='percent',
        value=df['10 Min Sampled Avg'].std(),
        thickness=1,
        width=0,
        color='#444',
        opacity=0.8
    )
)

data = [trace]
layout = go.Layout(
    yaxis=dict(title='Wind speed (m/s)'),
    title='Discrete, fixed value error bars
Notice the hover text!') fig = go.Figure(data=data, layout=layout) # IPython notebook # py.iplot(fig, filename='pandas-fixed-error-bars') url = py.plot(fig, filename='pandas-fixed-error-bars')
# Learn about API authentication here: https://plot.ly/pandas/getting-started
# Find your api_key here: https://plot.ly/settings/api

import plotly.plotly as py
import plotly.graph_objs as go
import pandas as pd
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/wind_speed_laurel_nebraska.csv')
df.head()

trace = go.Scatter(
    x=df['Time'],
    y=df['10 Min Sampled Avg'],
    mode='lines',
    error_y=dict(
        type='percent',
        array=df['10 Min Std Dev'],
        thickness=1,
        width=0,
        color='#444',
        opacity=0.8
    )
)
data = [trace]
layout = go.Layout(
    yaxis=dict(title='Wind speed (m/s)'),
    title='Discrete, variable value error bars
Notice the hover text!' ) fig = go.Figure(data=data, layout=layout) # IPython notebook # py.iplot(fig, filename='pandas-variable-error-bars') url = py.plot(fig, filename='pandas-variable-error-bars')
# Learn about API authentication here: https://plot.ly/pandas/getting-started
# Find your api_key here: https://plot.ly/settings/api

import plotly.plotly as py
import plotly.graph_objs as go
import pandas as pd
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/wind_speed_laurel_nebraska.csv')

upper_bound = go.Scatter(
    name='Upper Bound',
    x=df['Time'],
    y=df['10 Min Sampled Avg']+df['10 Min Std Dev'],
    mode='lines',
    marker=dict(color="444"),
    line=dict(width=0),
    fillcolor='rgba(68, 68, 68, 0.3)',
    fill='tonexty')

trace = go.Scatter(
    name='Measurement',
    x=df['Time'],
    y=df['10 Min Sampled Avg'],
    mode='lines',
    line=dict(color='rgb(31, 119, 180)'),
    fillcolor='rgba(68, 68, 68, 0.3)',
    fill='tonexty')

lower_bound = go.Scatter(
    name='Lower Bound',
    x=df['Time'],
    y=df['10 Min Sampled Avg']-df['10 Min Std Dev'],
    marker=dict(color="444"),
    line=dict(width=0),
    mode='lines')

# Trace order can be important
# with continuous error bars
data = [lower_bound, trace, upper_bound]

layout = go.Layout(
    yaxis=dict(title='Wind speed (m/s)'),
    title='Continuous, variable value error bars
Notice the hover text!', showlegend = False) fig = go.Figure(data=data, layout=layout) # IPython notebook # py.iplot(fig, filename='pandas-continuous-error-bars') url = py.plot(fig, filename='pandas-continuous-error-bars')
# Learn about API authentication here: https://plot.ly/pandas/getting-started
# Find your api_key here: https://plot.ly/settings/api

import plotly.plotly as py
from plotly.graph_objs import *
import pandas as pd

df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/tooth_growth_csv')
df2=df.groupby(['dose','supp']).describe()
df2.head()

supplements = tuple(df2.index.get_level_values('supp').unique())
doses = tuple(df2.index.get_level_values('dose').unique())

data = []

for supp in supplements:
    data.append(
        go.Bar(
            x = doses,
            y = df2.loc[doses,supp,'mean']['len'],

            error_y=dict(
                type='data',
                array=df2.loc[doses,supp,'std']['len']
            ),

            name = supp
        )
    )


layout = go.Layout( xaxis=XAxis(type='category') )
fig = Figure( data=data, layout=layout )

# IPython notebook
# py.iplot(fig, filename='pandas-error-bars-bar-chart')

url = py.plot(fig, filename='pandas-error-bars-bar-chart')
Still need help?
Contact Us

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