首页 > 解决方案 > 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?

标签: pythonpandasdataframematplotlibplot

解决方案


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


推荐阅读