mt82
mt82

Reputation: 1

Different binning for histplot as JoinGrid (x,y) marginal plot

I have a pandas dataframe like this:

Date Weight Year Month Day Week DayOfWeek
0 2017-11-13 76.1 2017 11 13 46 0
1 2017-11-14 76.2 2017 11 14 46 1
2 2017-11-15 76.6 2017 11 15 46 2
3 2017-11-16 77.1 2017 11 16 46 3
4 2017-11-17 76.7 2017 11 17 46 4
... ... ... ... ... ... ... ...

I created a JoinGrid with:

g = sns.JointGrid(data=df,
    x="Date",
    y="Weight",
    marginal_ticks=True,
    height=6, 
    ratio=2, 
    space=.05)

Then a defined joint and marginal plots:

g.plot_joint(sns.scatterplot,
        hue=df["Year"], 
        alpha=.4,
        legend=True)
g.plot_marginals(sns.histplot, 
    multiple="stack", 
    bins=20,
    hue=df["Year"])

Result is this.

result

Now the question is: "is it possible to specify different binning for the two histplot resulting in the x and y marginal plot?"

Upvotes: 0

Views: 420

Answers (1)

Diziet Asahi
Diziet Asahi

Reputation: 40737

I don't think there is a built-in way to do that, by you can plot directly on the marginal axes using the plotting function of your choice, like so:

penguins = sns.load_dataset('penguins')

data = penguins
x_col = "bill_length_mm"
y_col = "bill_depth_mm"
hue_col = "species"

g = sns.JointGrid(data=data, x=x_col, y=y_col, hue=hue_col)
g.plot_joint(sns.scatterplot)

# top marginal
sns.histplot(data=data, x=x_col, hue=hue_col, bins=5, ax=g.ax_marg_x, legend=False, multiple='stack')
# right marginal
sns.histplot(data=data, y=y_col, hue=hue_col, bins=40, ax=g.ax_marg_y, legend=False, multiple='stack')

enter image description here

Upvotes: 1

Related Questions