简体   繁体   English

如何在seaborn中使用'hue'参数绘制关节图

[英]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.如果在jointplot中实现了参数'hue'。

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.一个简单的替代方法是使用seaborn.lmplot - 即使未绘制 x 和 y 直方图。

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 ).此功能是在 2020 年 9 月的v0.11 Seaborn 版本中添加的(参见例如发布博客文章文档)。

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 ):我还想在联合图中给出一个核密度估计的最小例子(一个 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.并且近期不会实现,因为应该保留jointplot的简单性。

See here: https://github.com/mwaskom/seaborn/issues/365见这里: 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你只能做到一半(两个类都没有历史): 在 seaborn.jointplot 中绘制两个分布

Here is a solution using pairplot.这是使用pairplot的解决方案。

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

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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