Python 如何将错误栏添加到Matplotlib线图?

Python 如何将错误栏添加到Matplotlib线图?,python,dataframe,matplotlib,seaborn,data-visualization,Python,Dataframe,Matplotlib,Seaborn,Data Visualization,我有以下数据集,用于绘制线图。该图为从数据中获得的值的平均值。我想在这个图中添加误差条,它将显示标准偏差。我查阅了不同的答案,但在大多数答案中,他们明确地定义了x和y,但在这里,我直接从数据帧计算绘图。如何将错误条添加到此绘图 数据帧df UserId | date |-7|-6|-5|-4|-3|-2|-1|0 |1 |2 |3 |4 |5 |6 |7 1 2009-10-17 17:38:32.590 |0 |0 |0 |0 |

我有以下数据集,用于绘制线图。该图为从数据中获得的值的
平均值。我想在这个图中添加误差条,它将显示标准偏差。我查阅了不同的答案,但在大多数答案中,他们明确地定义了
x
y
,但在这里,我直接从数据帧计算绘图。如何将错误条添加到此绘图

数据帧df

UserId     |   date                 |-7|-6|-5|-4|-3|-2|-1|0 |1 |2 |3 |4 |5 |6 |7
     1      2009-10-17 17:38:32.590 |0 |0 |0 |0 |0 |0 |1 |0 |1 |0 |0 |0 |0 |0 |0  
     2      2009-10-19 00:37:23.067 |0 |0 |0 |0 |0 |1 |1 |0 |1 |0 |0 |0 |0 |0 |0    
     3      2009-10-20 08:37:14.143 |0 |0 |0 |0 |0 |0 |1 |0 |0 |0 |0 |0 |0 |0 |0 
     4      2009-10-21 18:07:51.247 |0 |0 |0 |0 |0 |0 |1 |0 |0 |0 |0 |0 |0 |0 |0 
     5      2009-10-22 21:25:24.483 |0 |0 |0 |0 |0 |0 |1 |0 |0 |0 |0 |0 |0 |0 |0
代码

badges = ["A", "B", "C"]

for badge in badges:
  res.iloc[:,2:].mean().plot(kind='line', label = badge)
输出

编辑(仅绘制一个带标准偏差的图形)

执行此代码会产生以下错误:

TypeError: 'value' must be an instance of str or bytes, not a int

如果要在循环中创建行和错误条,可以在循环中正常使用
plt.errorbar()
,但如果可以以适当的格式获取数据,则使用
seaborn
会容易得多

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

df = pd.concat([
    pd.DataFrame(np.random.randint(0,2, (5, 14))).assign(badge="A"),
    pd.DataFrame(np.random.randint(0,2, (5, 14))).assign(badge="B"),
])

plt.figure(figsize=(10, 5))

badges = ["A", "B"]
colors = ["teal", "firebrick"]

for i, badge in enumerate(badges):
    
    sub_df = df[df["badge"]==badge]
    
    sub_df.iloc[:,:-1].mean().plot(kind='line', label=badge, color=colors[i])

    plt.errorbar(
        sub_df.columns.values[:-1] + (i*0.1), # offset the second error bar a bit
        sub_df.iloc[:,:-1].mean(),
        yerr=sub_df.iloc[:,:-1].std(),
        fmt='|', ecolor='grey', elinewidth=1
    );

检查@Roim中的置信区间在这种情况下,我必须明确指定什么是
x
,什么是
y
,这在我的情况下是不可能的,因为您可以从代码本身看到。你能用代码来演示它吗?考虑到我没有使用循环并只绘制一个图形,如果我为errorbar
plt.errorbar(x=res.columns.values[:-1],y=res.iloc[:,2::.mean(),yerr=res.iloc[:,2:.std())
,我得到以下错误
TypeError:“value”必须是str或bytes的实例,不是整数
。如何修复此问题?您的示例代码显示了一个循环。你能编辑它来显示你遇到的问题的一个可复制的例子吗?我在最后添加了编辑,请看一看。当我在循环外运行我的答案中的代码时,我没有得到相同的错误。明白我所说的可复制的例子是什么意思吗。
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

df = pd.concat([
    pd.DataFrame(np.random.randint(0,2, (5, 14))).assign(badge="A"),
    pd.DataFrame(np.random.randint(0,2, (5, 14))).assign(badge="B"),
])

plt.figure(figsize=(10, 5))

badges = ["A", "B"]
colors = ["teal", "firebrick"]

for i, badge in enumerate(badges):
    
    sub_df = df[df["badge"]==badge]
    
    sub_df.iloc[:,:-1].mean().plot(kind='line', label=badge, color=colors[i])

    plt.errorbar(
        sub_df.columns.values[:-1] + (i*0.1), # offset the second error bar a bit
        sub_df.iloc[:,:-1].mean(),
        yerr=sub_df.iloc[:,:-1].std(),
        fmt='|', ecolor='grey', elinewidth=1
    );