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?
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"],
}
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With