Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Seaborn jointplot group colour coding (for both scatter and density plots)

Tags:

python

seaborn

I would like to use sns.jointplot to visualise the association between X and Y in the presence of two groups. However, in

tips = sns.load_dataset("tips")
sns.jointplot("total_bill", "tip", data=tips) 

enter image description here

there is no "hue" option as in other sns plots such as sns.scatterplot. How could one assign different colours for different groups (e.g. hue="smoker") in both the scatter plot, as well as the two overlapping density plots.

In R this could be done by creating a scatter plot with two marginal density plots as shown in here. enter image description here

What is the equivalent in sns? If this is not possible in sns, is there another python package that can be used for this?

like image 743
user1442363 Avatar asked Apr 24 '19 22:04

user1442363


1 Answers

jointplot is a simple wrapper around sns.JointGrid. If you create a JointGrid object and add plots to it manually, you will have much more control over the individual plots.

In this case, your desired jointplot is simply a scatterplot combined with a kdeplot, and what you want to do is pass hue='smoker' (for example) to scatterplot.

The kdeplot is more complex; seaborn doesn't really support one KDE for each class, AFAIK, so I was forced to plot them individually (you could use a for loop with more classes).

Accordingly, you can do this:

import seaborn as sns

tips = sns.load_dataset('tips')
grid = sns.JointGrid(x='total_bill', y='tip', data=tips)

g = grid.plot_joint(sns.scatterplot, hue='smoker', data=tips)
sns.kdeplot(tips.loc[tips['smoker']=='Yes', 'total_bill'], ax=g.ax_marg_x, legend=False)
sns.kdeplot(tips.loc[tips['smoker']=='No', 'total_bill'], ax=g.ax_marg_x, legend=False)
sns.kdeplot(tips.loc[tips['smoker']=='Yes', 'tip'], ax=g.ax_marg_y, vertical=True, legend=False)
sns.kdeplot(tips.loc[tips['smoker']=='No', 'tip'], ax=g.ax_marg_y, vertical=True, legend=False)

enter image description here

like image 194
gmds Avatar answered Sep 22 '22 04:09

gmds