Show Sidebar Hide Sidebar

Error Bars in matplotlib

How to add error bars. Examples of different types of error bars from matplotlib.

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!

Version Check

Plotly's python package is updated frequently. Run pip install plotly --upgrade to use the latest version.

In [1]:
import plotly
plotly.__version__
Out[1]:
'3.1.1'

Basic Symmetric Error Bars

Inspired by Matplotlib Docs

In [2]:
import plotly.plotly as py
import plotly.tools as tls

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(0.1, 4, 0.5)
y = np.exp(-x)

plt.figure()
plt.plot(x, y, '-')
plt.title("Simplest errorbars, 0.04 in x, 0.04 in y")

fig = plt.gcf()
plotly_fig = tls.mpl_to_plotly( fig )

plotly_fig["data"][0]["error_y"].update({
                                         "visible": True,
                                         "color":"rgb(255,127,14)",
                                         "value":0.04,
                                         "type":"constant"
                                       })
plotly_fig["data"][0]["error_x"].update({
                                         "visible": True,
                                         "color":"rgb(255,127,14)",
                                         "value":0.04,
                                         "type":"constant"
                                       })

py.iplot(plotly_fig, filename='mpl-simple-error-bars')
Out[2]:

Assymetric Error Bars

In [3]:
import plotly.plotly as py
import plotly.tools as tls

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(0.1, 4, 0.5)
y = np.exp(-x)

plt.figure()
plt.plot(x, y, '-')
plt.title("Assymetric Error bars")

error_x = [0.1,0.2, 0.1, 0.1, 0.3, 0.05, 0.8]
error_y = [0.1,0.2, 0.1, 0.1, 0.3, 0.05, 0.8]

fig = plt.gcf()
plotly_fig = tls.mpl_to_plotly( fig )
plotly_fig["data"][0]["error_y"].update({
                                         "visible": True,
                                         "color":"rgb(255,127,14)",
                                         "array":error_y
                                       })
plotly_fig["data"][0]["error_x"].update({
                                        "visible": True,
                                        "color":"rgb(255,127,14)",
                                        "array":error_x
                                      })
py.iplot(plotly_fig, filename='mpl-assymetric-error-bars')
Out[3]:

Reference

See https://plot.ly/python/reference/ for more information and chart attribute options!

Still need help?
Contact Us

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