Test
Test

Reputation: 549

Group by and mean inside seaborn plot

I wonder how I could group by and mean a value inside of a seaborn plot. Is there an option to do this df.groupby('month')['temperature'].mean().index df.groupby('month')['temperature'].mean().values inside a seaborn plot without generate a new dataframe like df2 = df.groupby('month')['temperature'].mean()

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

d = {'id': [1, 2, 3, 4, 5], 'month': [1, 2, 3, 4, 2], 'temperature': [20, 40, 50, 60, 20]}
df = pd.DataFrame(data=d)

x = df.groupby('month')['temperature'].mean().index
y = df.groupby('month')['temperature'].mean().values


plt.plot(x,y)
plt.show()

 # With the below code I got the desired output 

df2 = df.groupby('month')['temperature'].mean()
sns.lineplot(x=df2.index, y=df2.values,  data=df2)

Upvotes: 1

Views: 3711

Answers (1)

mwaskom
mwaskom

Reputation: 48992

I'm not sure exactly what you mean by "group by and mean a value inside of a seaborn plot".

One thing you could mean is "I want to do that operation but don't want to assign its output to a variable", in which case you can inline it with the assignment to seaborn's data parameter and refer to the index/column by their names. But to do this, you need to convert the output of your groupby, which is a pandas Series, back to a dataframe:

sns.lineplot(
    x="month", y="temperature",
    data=df.groupby('month')['temperature'].mean().to_frame(),  # or .reset_index()
)

But if you want to do a line plot from a series where the x variable gets the index and the y variable gets the values, you can pass the Series to data= and not bother with assigning x=, y=:

sns.lineplot(data=df.groupby('month')['temperature'].mean(), linestyle="--")

Another thing you could be asking is "if I give seaborn the original dataframe can I get it to do the groupby/aggregation for me?" in which case the answer is yes and it's the default behavior / simplest invocation:

sns.lineplot(data=df, x="month", y="temperature", ci=None, linestyle=":")

These all produce the same result:

enter image description here

Upvotes: 2

Related Questions