Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 944
Pablo Avatar asked Nov 04 '16 10:11

Pablo


People also ask

How do you use hue in Seaborn?

In seaborn, the hue parameter determines which column in the data frame should be used for colour encoding. Using the official document for lmplot provided an example for this. Adding `hue="smoker" tells seaborn you want to colour the data points for smoker and non-smoker differently.

How do you change hue color in Seaborn scatter plot?

Scatterplot with Seaborn Default Colors In addition to these arguments we can use hue and specify we want to color the data points based on another grouping variable. This will produce points with different colors. g =sns. scatterplot(x="gdpPercap", y="lifeExp", hue="continent", data=gapminder); g.

How do you plot a Jointplot?

A Jointplot comprises three plots. Out of the three, one plot displays a bivariate graph which shows how the dependent variable(Y) varies with the independent variable(X). Another plot is placed horizontally at the top of the bivariate graph and it shows the distribution of the independent variable(X).

What is hue in Python Seaborn?

hue : (optional) This parameter take column name for colour encoding. data : (optional) This parameter take DataFrame, array, or list of arrays, Dataset for plotting. If x and y are absent, this is interpreted as wide-form. Otherwise it is expected to be long-form.


1 Answers

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

like image 146
Manu CJ Avatar answered Sep 22 '22 06:09

Manu CJ