Seaborn Facetgrid计数图色调 [英] Seaborn Facetgrid countplot hue

查看:62
本文介绍了Seaborn Facetgrid计数图色调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为此请求创建了一个样本数据集

I have created a sample data set for this quesition

import pandas as pd
from pandas import DataFrame
import seaborn as sns
import numpy as np


sex = np.array(['Male','Female'])
marker1 = np.array(['Absent','Present'])
marker2 = np.array(['Absent','Present'])

sample1 = np.random.randint(0,2,100)
sample2 = np.random.randint(0,2,100)
sample3 = np.random.randint(0,2,100)

df=pd.concat([pd.Series(sex.take(sample1),dtype='category'),pd.Series(marker1.take(sample2),dtype='category'),pd.Series(marker2.take(sample3),dtype='category')],axis=1)

df.rename(columns={0:'Sex',1:'Marker1',2:'Marker2'},inplace=True)

fig =sns.FacetGrid(data=df,col='Sex',hue='Marker2',palette='Set1',size=4,aspect=1).map(sns.countplot,'Marker1',order=df.Marker1.unique()).add_legend()

此代码创建的图是堆积图 我要创建的是一个躲避图(来自R).如何修改此代码,以便可以对 Marker2 存在进行并排比较?

The plot this code creates is a stacked plot What I want to create is a dodge plot (from R). How can I modify this code so that I can see a side by side comparison of Marker2 presence?

推荐答案

我遇到了同样的问题,并且对我有用.

I had the same problem and this worked for me.

您可以为sns.countplot调用编写包装函数,以使用FacetGrid命令.

You can just write a wrapper function for your sns.countplot call to use the FacetGrid command.

def countplot(x, hue, **kwargs):
    sns.countplot(x=x, hue=hue, **kwargs)

grid = sns.FacetGrid(data=df,col='Sex',size=4,aspect=1)
fig = grid.map(countplot,'Marker1','Marker2',palette='Set1',order=df.Marker1.unique())
fig.add_legend()

这篇关于Seaborn Facetgrid计数图色调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆