Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add error bar to a single column in pandas plot

I have a dataframe which looks like this:

df = pd.DataFrame({'Pred': [10, 9.5, 9.8], 'Actual': [10.2, 9.9, 9.1], 'STD': [0.1, 0.2, 0.6]})

    Pred    Actual  STD
0   10.0    10.2    0.1
1   9.5     9.9     0.2
2   9.8     9.1     0.6

I want to make a bar plot with error bars using STD only on the Pred column, and not on the Actual column. So far I have this:

df.plot.bar(yerr='STD', capsize=4)

enter image description here

but this adds the error bars on both Actual and Pred. Is there a straight forward way to tell Pandas to add the erorr bar to a single column?

like image 876
Gerges Avatar asked Dec 06 '25 18:12

Gerges


1 Answers

You can do with

import matplotlib.pyplot as plt
fig, ax = plt.subplots()
errors=df.STD.to_frame('Pred')
df[['Actual','Pred']].plot.bar(yerr=errors, ax=ax)

enter image description here

like image 137
BENY Avatar answered Dec 09 '25 17:12

BENY



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!