0
$\begingroup$

I have the following dataset which I use to plot a line plot. The plot is obtained as the mean of values obtained from the data. I want to add error bars to this plot which shall show the standard deviation. I have looked up to different answers but in most of them they had defined x and y explicitly, but here I calculate the plot directly from the dataframe. How to add error bar to this plot?

Dataframe 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

Code

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

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

Output

Output Obtained

$\endgroup$
0
$\begingroup$

Why not compute the x values explicitly? In the dataframe above,

x = [c for c in res.columns() if isinstance(int, c)]

Then, the code can be as follows.

badges = ["A", "B", "C"]
fig, ax = plt.subplots()
for badge in badges:
    means = res.iloc[:,2:].mean()
    std = res.iloc[:, 2:].std()
    means.plot(kind='line', label = badge)

    ax.plot(means)
    ax.fill_between(x, means.sub(std), means.add(std), color='b', alpha=.1)

Note: In the code above, you use badges only in the plot. Maybe computing means and std should be done outside the cycle? I am not sure because your sample data frame above does not have badges.

$\endgroup$

This site is temporarily in read only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .