How to Create Error Bars in Plotly Python?

Spread the love

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()
fig.add_trace(go.Bar(
    name='Control',
    x=['Trial 1', 'Trial 2', 'Trial 3'], y=[3, 6, 4],
    error_y=dict(type='data', array=[1, 0.5, 1.5])
))
fig.add_trace(go.Bar(
    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 –

  1. How to install plotly python with pip?
  2. How to create a Line Chart with Plotly Python?
  3. How to create Scatter plot in Plotly Python?
  4. How to create a Bar Chart in Plotly Python?
  5. How to create Horizontal Bar Chart in Plotly Python?
  6. How to create a Histogram in plotly python?
  7. How to Create a Box Plot in Plotly Python?
  8. How to create a Pie Chart in Plotly Python?
  9. How to create a Dot Plot in Plotly Python?
  10. How to Create Heatmap with Plotly Python?
  11. How to Create a Violin Plot in Plotly Python?
  12. How to Create Subplots in Plotly Python?
  13. How to Create a Bubble Chart in Plotly Python?
  14. How to Create a Gantt Chart in Plotly Python?
  15. How to Create an Area Chart in Plotly Python?
  16. How to Create Tables in Plotly Python?
  17. How to Create a Sunburst Chart in Plotly Python?
  18. How to Create a Sankey Diagram in Plotly Python?
  19. How to Create a Treemap Charts in Plotly Python?

Rating: 1 out of 5.

Leave a Reply