如何在 seaborn 中使用 'hue' 参数绘制联合图

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

我想要以下命令行的情节:

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

如果参数'hue'是在jointplot中实现的。

我该怎么做?

也许叠加两个联合地块?

一个简单的替代方法是使用 seaborn.lmplot——即使没有绘制 x 和 y 直方图。

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

你不能,很遗憾

近期不会实现,因为要保留jointplot的简洁性

看这里:https://github.com/mwaskom/seaborn/issues/365

你只能做一半(两者都没有 hist 类):

这是一个使用 pairplot 的解决方案。

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

此功能已添加到 v0.11 Seaborn release in September 2020 (see e. g. the release blog post or the documentation)。

文档现在提供了一个基于企鹅数据集的很好的例子:

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

我还想举一个联合图中核密度估计的最小例子(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})