简体   繁体   中英

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?

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)

在此处输入图片说明

This functionality was added in thev0.11 Seaborn release in September 2020 (see eg 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联合图示例

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})

带有 kdeplot 的 seaborn jointplot 示例

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

Here is a solution using pairplot.

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

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM