在pandas绘图中为单个列添加误差条

3

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

我想用STDPred列上制作带误差线的柱状图,而不是在Actual列上。到目前为止我有以下内容:

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

但是这将在实际预测两个列上都添加误差线。有没有一种简单明了的方法告诉Pandas只在一个列上添加误差线?

enter image description here

1个回答

4
您可以使用以下方法进行操作:
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


3
谢谢,这很有帮助。我最终使用了 df.drop('STD', axis=1).plot.bar(yerr={'Pred': df.STD}),这样我就不需要指定 ['Actual', 'Pred'] 了,这正是我想要避免的。 - Gerges

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接