gpt4 book ai didi

python - Seaborn Facetgrid countplot 色调

转载 作者:行者123 更新时间:2023-11-30 22:55:39 28 4
gpt4 key购买 nike

我为此问题创建了一个示例数据集

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

此代码创建的图是堆积图 enter image description here我想创建的是闪避图(来自 R)。如何修改此代码以便可以看到 Marker2 存在情况的并排比较?

最佳答案

我遇到了同样的问题,这对我有用。

您只需为 sns.countplot 调用编写一个包装函数即可使用 FacetGrid 命令。

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

关于python - Seaborn Facetgrid countplot 色调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37331937/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com