Pablo
Pablo

Reputation: 352

How to plot a jointplot with 'hue' parameter in seaborn

I would like to have the plot of the following command line:

import numpy as np, pandas as pd
import seaborn as sns; sns.set(style="white", color_codes=True)
tips = sns.load_dataset("tips")
g = sns.jointplot(x="total_bill", y="tip", data=tips, hue= 'sex')

if the parameter 'hue' was implemented in jointplot.

How can I do this?

Maybe superposing two joint plots?

Upvotes: 14

Views: 25830

Answers (4)

Manu J4
Manu J4

Reputation: 2859

This functionality was added in the v0.11 Seaborn release in September 2020 (see e. g. the release blog post or the documentation).

The documentation now features a great example based on the penguins dataset:

penguins = sns.load_dataset("penguins")
sns.jointplot(data=penguins, x="bill_length_mm", y="bill_depth_mm", hue="species")

seaborn jointplot example with scatterplot

I further would like to give a minimal example for a Kernel density estimation in the joint plot (a 2d kdeplot):

# optional: sns.set(style='darkgrid')
data = {'x': [1, 2, 3, 4, 5, 6], 
        'y': [2, 4, 1.5, 4, 3, 5], 
        'class': ['1', '1', '1', '0', '0', '0']}
sns.jointplot(data=data, x='x', y='y', hue='class', kind='kde',
              fill=True, joint_kws={'alpha': 0.7})

seaborn jointplot example with kdeplot

Upvotes: 11

Elinor Medezinski
Elinor Medezinski

Reputation: 51

Here is a solution using pairplot.

g = sns.pairplot(data=tips[['total_bill','tip','sex']], hue='sex', corner=True, )

enter image description here

Upvotes: 1

Mayou36
Mayou36

Reputation: 4870

You can't, unfortunately

and it won't be implemented in the near future, because the simplicity of jointplot should be preserved.

See here: https://github.com/mwaskom/seaborn/issues/365

You can only do it halfway (without the hist for both classes): Plotting two distributions in seaborn.jointplot

Upvotes: 6

Romain
Romain

Reputation: 21888

A simple alternative is to use seaborn.lmplot -- even if x and y histogram are not drawn.

sns.lmplot(x='total_bill', y='tip', hue='sex', data=tips, fit_reg=False)

enter image description here

Upvotes: 13

Related Questions