0
votes

I have a data frame with one column that describes y-axis values and two more columns that describe the upper and lower bounds of a confidence interval. I would like to use those values to draw error bars using plotly. Now I am aware that plotly offers the possibility to draw confidence intervals (using the error_y and error_y_minus keyword-arguments) but not in the logic that I need, because those keywords are interpreted as additions and subtractions from the y-values. Instead, I would like to directly define the upper and lower positions:

For instance, how could I use plotly and this example data frame

import pandas as pd
import plotly.express as px

df = pd.DataFrame({'x':[0, 1, 2],
                   'y':[6, 10, 2],
                   'ci_upper':[8,11,2.5],
                   'ci_lower':[5,9,1.5]})

to produce a plot like this?

enter image description here

1

1 Answers

0
votes
import pandas as pd
import plotly.express as px

df = pd.DataFrame(
    {"x": [0, 1, 2], "y": [6, 10, 2], "ci_upper": [8, 11, 2.5], "ci_lower": [5, 9, 1.5]}
)


px.bar(df, x="x", y="y").update_traces(
    error_y={
        "type": "data",
        "symmetric": False,
        "array": df["ci_upper"] - df["y"],
        "arrayminus": df["y"] - df["ci_lower"],
    }
)

enter image description here