9

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?

user1442363
  • 800
  • 1
  • 10
  • 18
  • Some relevant links * https://github.com/mwaskom/seaborn/issues/365, * https://stackoverflow.com/questions/51210955/seaborn-jointplot-add-colors-for-each-class, * https://stackoverflow.com/questions/31539815/plotting-two-distributions-in-seaborn-jointplot?noredirect=1&lq=1 – ImportanceOfBeingErnest Apr 24 '19 at 22:59
  • thank you. I've seen the first and the last questions but the second didn't come up in my search. – user1442363 Apr 25 '19 at 08:41

1 Answers1

17

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

gmds
  • 19,325
  • 4
  • 32
  • 58
  • 2
    At the time of this answer this may not have been possible in sns.jointplot, but in version 0.11.1 this is natively supported with the "hue" argument. http://seaborn.pydata.org/generated/seaborn.jointplot.html – Bow Jun 17 '21 at 16:25