0

Is there a way to both bin and aggregate (with some other function than count) in seaborn.objects? I'd like to compute the mean per bin and right now I'm using the following:

import seaborn.objects as so
import pandas as pd
import seaborn as sns

df = sns.load_dataset("penguins")
df2 = (
    df.groupby(pd.cut(df["bill_length_mm"], bins=30))[["bill_depth_mm"]]
    .mean()
)
df2["bill_length_mm"] = [x.mid for x in df2.index]
p = so.Plot(df2, x="bill_length_mm", y="bill_depth_mm").add(so.Bars())

p

1 Answer 1

3

There's not yet a binning operation separate from Hist (this could make sense as a Stat or a Scale, I'm not sure).

But note that you can do the aggregation more simply than you are in your example because you can pass a Series directly and don't need to construct a new dataframe:

(
    so.Plot(
        df,
        x="bill_depth_mm",
        y=pd.cut(df["bill_length_mm"], bins=30).map(lambda x: x.mid),
    )
    .add(so.Bars(), so.Agg("mean"))
)

Note that the Series will be aligned with the DataFrame (or other Series passed directly) based on the index information rather than position.

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! "Note that the Series will be aligned with the DataFrame (or other Series passed directly) based on the index information rather than position." ... I'm not sure what this means :)
e.g., s1 = pd.Series([1, 2, 3], index=[*'ABC']) and s2 = pd.Series([1, 2, 3], index=[*'BCA']) would align as 1:3, 2:1, 3:2 (based on A, B, C) rather than 1:1, 2:2, 3:3 (based on position in array)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.