简体   繁体   English

Seaborn jointplot组颜色编码(用于散点图和密度图)

[英]Seaborn jointplot group colour coding (for both scatter and density plots)

I would like to use sns.jointplot to visualise the association between X and Y in the presence of two groups. 我想使用sns.jointplot在存在两个组的情况下可视化X和Y之间的关联。 However, in 但是,在

tips = sns.load_dataset("tips")
sns.jointplot("total_bill", "tip", data=tips) 

在此输入图像描述

there is no "hue" option as in other sns plots such as sns.scatterplot. 没有像其他sns图中那样的“hue”选项,例如sns.scatterplot。 How could one assign different colours for different groups (eg hue="smoker") in both the scatter plot, as well as the two overlapping density plots. 如何在散点图以及两个重叠的密度图中为不同的组(例如hue =“吸烟者”)分配不同的颜色。

In R this could be done by creating a scatter plot with two marginal density plots as shown in here . 在该R这可以通过如创建两个边缘密度图散点图来完成 在此输入图像描述

What is the equivalent in sns? 什么是sns中的等价物? If this is not possible in sns, is there another python package that can be used for this? 如果这在sns中是不可能的,那么还有另一个可用于此的python包吗?

jointplot is a simple wrapper around sns.JointGrid . jointplot是围绕一个简单的包装sns.JointGrid If you create a JointGrid object and add plots to it manually, you will have much more control over the individual plots. 如果您创建一个JointGrid对象并手动添加绘图,您将可以更好地控制各个绘图。

In this case, your desired jointplot is simply a scatterplot combined with a kdeplot , and what you want to do is pass hue='smoker' (for example) to scatterplot . 在这种情况下,您所需的jointplot只是一个scatterplotkdeplot相结合,您想要做的是将hue='smoker' (例如)传递给scatterplot

The kdeplot is more complex; kdeplot更复杂; seaborn doesn't really support one KDE for each class, AFAIK, so I was forced to plot them individually (you could use a for loop with more classes). seaborn并不真正支持每个类的一个KDE,AFAIK,所以我被迫单独绘制它们(你可以使用更多类的for循环)。

Accordingly, you can do this: 因此,您可以这样做:

import seaborn as sns

tips = sns.load_dataset('tips')
grid = sns.JointGrid(x='total_bill', y='tip', data=tips)

g = grid.plot_joint(sns.scatterplot, hue='smoker', data=tips)
sns.kdeplot(tips.loc[tips['smoker']=='Yes', 'total_bill'], ax=g.ax_marg_x, legend=False)
sns.kdeplot(tips.loc[tips['smoker']=='No', 'total_bill'], ax=g.ax_marg_x, legend=False)
sns.kdeplot(tips.loc[tips['smoker']=='Yes', 'tip'], ax=g.ax_marg_y, vertical=True, legend=False)
sns.kdeplot(tips.loc[tips['smoker']=='No', 'tip'], ax=g.ax_marg_y, vertical=True, legend=False)

在此输入图像描述

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

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