Python中按附加类别绘制Facet box

2024-04-25 23:57:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我用Python创建了一个带有Seaborn包(sns)的方框图,如下面的代码所示,其中x轴是年龄范围,y轴是美元值('AveMonthSpend')。你知道吗

def plot_box(df, col, col_y ='AveMonthSpend'):
    sns.set_style("whitegrid")
    sns.boxplot(col, col_y, data=df)
    plt.xlabel(col) # Set text for the x axis
    plt.ylabel(col_y)# Set text for y axis
    plt.show()

plot_box(df_3, 'age_range')  

我想做的是用第三列来刻面这个方框图-'Gender',两个值是['M','F']。我读过这个网站,也尝试过很多选择,但似乎都不管用。你知道吗

Click here for image of box plots

以下是我尝试的选项之一:

g = sns.FacetGrid(pd.melt(df, id_vars='Gender'), col='Gender')
g.map(sns.boxplot, 'age_range', 'AveMonthSpend')

你知道吗 为此,我得到了以下错误:

KeyError: "['age_range' 'AveMonthSpend'] not in index"  

如有任何建议,将不胜感激。谢谢您!你知道吗


Tags: textboxdfforageplotrangeplt