user6594048
user6594048

Reputation: 35

How to plot the average of a curve with seaborn?

I would like to plot the average of some points and their interval of confidence. I am using the seaborn. I tried to use regplot, the lmplot, but all of them do not work. This is my program:

import pandas as pd
from matplotlib import pyplot as plt
import seaborn as sns
sns.set_style("darkgrid")
sns.set(color_codes=True)

df = pd.read_csv("data.csv")
print(df["a"])
sns.lineplot(y=df.a, x=df.index , data=df)
plt.ylabel("R")
plt.xlabel("T")
plt.show()

img

My goal is to get the red line in the figure. This is a simple of dataframe as saved in csv Fila

index     a
0      -0.120
1      -0.530
2      -0.250
3      -0.330
4      -0.560
5      -0.260
6       0.018
7       0.040
8      -0.460
9      -0.690
10     -0.130
11     -0.270
12     -0.080
13     -0.430
14     -0.170
15     -0.500
16     -0.690
17     -0.060
18     -0.200
19     -0.610
20     -0.370
21      0.090
22      0.190
23     -0.010
24      0.110
25     -0.250
26     -0.210
27     -0.160
28     -0.320
29      0.130
        ...  
2501    0.760
2502    0.690
2503    0.680
2504    0.750
2505    0.560
2506    0.570
2507    0.670
2508    0.800
2509    0.630
2510    0.570
2511    0.780
2512    0.800
2513    0.800

Upvotes: 2

Views: 3747

Answers (1)

Baruch
Baruch

Reputation: 41

Mean is an aggregattive function. It gets a few values and aggragates them into one.
In your case, since there is only one value per index, a mean aggragation will output a single value for the hole dataset, a single value cannot be plotted of course. Therefore, Seaborn library plots an average line Only when it needs to aggragate a few values per index In this case Seaborn will also provide you with a corresponding confidence interval. An example for a command that would work:

Dataset is appended as a picture

Dataset

when calling sns.lineplot(y='Flights', x='Week' , data=df)

Seaborn has to aggragate a few values for each week in order to plot the data. The default aggragation method is 'mean', so this aggragation will result at an average line with confidence interval.

You can choose any Series aggragation method provided by Pandas, you can also write your own methods

Upvotes: 4

Related Questions