# How to Create Error Bars in Plotly Python?

In this post you will learn How to Create Error bars in Plotly Python.

## Error Bars –

Error bars are graphical representations of the variability of data and used on graphs to indicate the error or uncertainty in a reported measurement. They give a general idea of how precise a measurement is, or conversely, how far from the reported value the true value might be.

## Error Bars with Plotly Graph Objects –

### Creating Symmetric Error Bars –

Let’s create a symmetric error bars on a line chart. To create a error bars on a line chart we use the error_y parameter of go.Scatter function. To Learn more about line charts – click here

``````import plotly.graph_objects as go

fig = go.Figure(data=go.Scatter(
x = [0, 1, 2],
y = [5, 10, 2],
error_y = dict(
type='data',
array = [1, 2, 3]
)
))
fig.show()``````

Since this is a symmetric error bars, we only need to define the array parameter in error_y. We also need to set type=’data’.

### Creating Asymmetric Error Bars –

To create asymmetric error bars, we need to define array parameter as well as arrayminus parameter of the error_y and set symmetric=False.

``````fig = go.Figure(data=go.Scatter(
x=[1, 2, 3, 4],
y=[2, 1, 3, 4],
error_y=dict(
type='data',
symmetric=False,
array=[0.1, 1.5, 0.1, 0.1],
arrayminus=[0.2, 0.6, 1, 0.2])
))
fig.show()``````

### Error Bars as Percentage of the Y Value –

We can also create error bars as the percentage of the Y value. For this we need to set the type parameter to percent. And provide the percentage value using the value parameter.

``````fig = go.Figure(data=go.Scatter(
x=[1, 2, 3, 4],
y=[2, 1, 3, 4],
error_y=dict(
type='percent',
value=50) # 50% of y values
))
fig.show()``````

### Asymmetric Error Bars with Percentage of Y Value –

If you want to create an asymmetric error bars with percentage of y values then you need to set the value and valueminus parameters.

``````fig = go.Figure(data=go.Scatter(
x=[1, 2, 3, 4],
y=[2, 1, 3, 4],
error_y=dict(
type='percent',
symmetric=False,
value=50,
valueminus=25)
))
fig.show()``````

### Horizontal Error Bars –

You can also create horizontal Error Bars. For this you need to use the error_x parameter of the go.Scatter function instead of error_y.

``````fig = go.Figure(data=go.Scatter(
x=[1, 2, 3, 4],
y=[2, 1, 3, 4],
error_x=dict(
type='percent',
value=10)
))
fig.show()``````

### Error Bars in a Bar Charts –

You can also create error bars in a bar chart using the error_y parameter of the go.Bar function. To Learn more about Bar charts – click here.

``````import plotly.graph_objects as go

fig = go.Figure()
name='Control',
x=['Trial 1', 'Trial 2', 'Trial 3'], y=[3, 6, 4],
error_y=dict(type='data', array=[1, 0.5, 1.5])
))
name='Experimental',
x=['Trial 1', 'Trial 2', 'Trial 3'], y=[4, 7, 3],
error_y=dict(type='data', array=[0.5, 1, 2])
))
fig.update_layout(barmode='group')
fig.show()``````

## Error Bars using Plotly Express –

You can also create error bars using plotly express. Let’s create a error bar in a scatter plot. The scatter plot function px.Scatter also have the parameters error_x and error_y to define the error bars. To Learn more about scatter plots – click here

``````import plotly.express as px
df = px.data.iris()
df["e"] = df["sepal_width"]/100
fig = px.scatter(df, x="sepal_width", y="sepal_length", color="species",
error_x="e", error_y="e")
fig.show()``````

### Asymmetric Error Bars with Plotly Express –

You can also create asymmetric error bars in plotly express.

``````import plotly.express as px
df = px.data.iris()
df["e_plus"] = df["sepal_width"]/100
df["e_minus"] = df["sepal_width"]/40
fig = px.scatter(df, x="sepal_width", y="sepal_length", color="species",
error_y="e_plus", error_y_minus="e_minus")
fig.show()``````

### Related Posts –

Rating: 1 out of 5.