Show Sidebar Hide Sidebar

Error Bars in matplotlib

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

import matplotlib.pyplot as plt
import numpy as np

import plotly.plotly as py
import plotly.tools as tls
# Learn about API authentication here: https://plot.ly/python/getting-started
# Find your api_key here: https://plot.ly/settings/api


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"
                                       })

plot_url = py.plot(plotly_fig, filename='mpl-simple-error-bars')
Inspired by Matplotlib Docs
import matplotlib.pyplot as plt
import numpy as np

import plotly.plotly as py
import plotly.tools as tls
# Learn about API authentication here: https://plot.ly/python/getting-started
# Find your api_key here: https://plot.ly/settings/api

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
                                      })
plot_url = py.plot(plotly_fig, filename='mpl-assymetric-error-bars')
Still need help?
Contact Us

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