【问题标题】:Add error bar to a single column in pandas plot将误差线添加到熊猫图中的单列
【发布时间】:2019-05-03 04:23:39
【问题描述】:

我有一个如下所示的数据框:

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

我想仅在 Pred 列上而不是在 Actual 列上使用 STD 制作带有误差线的条形图。到目前为止,我有这个:

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

但这会在ActualPred 上添加错误栏。有没有直接的方法告诉 Pandas 将错误栏添加到单列?

【问题讨论】:

    标签: python pandas dataframe matplotlib plot


    【解决方案1】:

    你可以用

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

    【讨论】:

    • 谢谢,这很有帮助。我最终使用了df.drop('STD', axis=1).plot.bar(yerr={'Pred': df.STD}),这样我就不必指定['Actual', 'Pred'],这是我试图避免的。
    猜你喜欢
    • 2014-05-24
    • 1970-01-01
    • 2018-06-04
    • 1970-01-01
    • 2019-04-27
    • 2016-01-04
    • 2021-01-25
    • 2020-09-08
    • 2023-01-17
    相关资源
    最近更新 更多