14

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?

Archie
  • 2,247
  • 1
  • 18
  • 35
Pablo
  • 312
  • 1
  • 3
  • 11
  • 4
    Did you check already [this answer](http://stackoverflow.com/a/31543774/5741205)? You may also want to check [this solution from seaborn's author](https://github.com/mwaskom/seaborn/issues/294) – MaxU - stand with Ukraine Nov 04 '16 at 10:14

4 Answers4

13

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

Romain
  • 19,910
  • 6
  • 56
  • 65
11

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

Manu CJ
  • 2,629
  • 1
  • 18
  • 29
  • it's throwing an error, `unexpected keyword argument` first alpha remove it next fill remove it next hue and finally worked I truly don't know why can't use `kind` with `hue` (notice I'm using `reg` for kind) – Walid Bousseta Sep 02 '21 at 06:03
6

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

Community
  • 1
  • 1
Mayou36
  • 4,613
  • 2
  • 17
  • 20
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/14343204) – eko Nov 19 '16 at 21:49
  • Hm, so what does the link say if not "no" and "you won't be able to do it in the future" and "because the simplicity of jointplot should be preserved"? My answer fully summarizes what the link says, the link itself is my source... So what would you want to change? – Mayou36 Nov 20 '16 at 11:11
  • @echonax,the answer is here, you can't do it. The link just supports my statement. What should I change then? – Mayou36 Jul 27 '17 at 13:08
  • It's interesting to see how @Mayou36 concludes this won't be implemented in the near future and then in less than a year, the feature is indeed implemented! – rileymcdowell Dec 17 '20 at 19:42
1

Here is a solution using pairplot.

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

enter image description here