2017-07-06 81 views
1

我正在与泰坦尼克号的乘客数据集(从Kaggle)一起作为Udacity课程的一部分。我使用Seaborn FacetGrid来查看Travel类和性别的乘客年龄分布概况 - 色调为'Survived'(1/0)。在Seaborn FacetGrid图上绘制不同'色调'数据的平均线

情节运行良好,我想为每个子区域添加垂直平均线 - 但是对于每个子区域(1/0)中两个“色调”中的每一个,使用不同的颜色(以及不同的注释)。下面代码中的'vertical_mean_line'函数在没有多个“色调”数据的情节中效果很好 - 但我无法找到为每种色调绘制不同线条的方法

任何想法如果可以在Seaborn中执行此操作?

电流Seaborn FacetGrid情节输出:

Seaborn FacetGrid plot

代码:

sns.set() 
sns.set_context('talk') 
sns.set_style('darkgrid') 
grid = sns.FacetGrid(titanic_data.loc[titanic_data['is_child_def'] == False], col='Sex', row = 'Pclass', hue='Survived' ,size=3.2, aspect=2) 
grid.map(sns.kdeplot, 'Age', shade=True) 
grid.set(xlim=(14, titanic_data['Age'].max()), ylim=(0,0.06)) 
grid.add_legend() 


# Add vertical lines for mean age on each plot 
def vertical_mean_line_survived(x, **kwargs): 
    plt.axvline(x.mean(), linestyle = '--', color = 'g') 
    #plt.text(x.mean()+1, 0.052, 'mean = '+str('%.2f'%x.mean()), size=12) 
    #plt.text(x.mean()+1, 0.0455, 'std = '+str('%.2f'%x.std()), size=12) 

grid.map(vertical_mean_line_survived, 'Age') 

# Add text to each plot for relevant popultion size 
# NOTE - don't need to filter on ['Age'].isnull() for children, as 'is_child'=True only possible for children with 'Age' data 
for row in range(grid.axes.shape[0]): 
    grid.axes[row, 0].text(60.2, 0.052, 'Survived n = '+str(titanic_data.loc[titanic_data['Pclass']==row+1].loc[titanic_data['is_child_def']==False].loc[titanic_data['Age'].isnull()==False].loc[titanic_data['Survived']==1]['is_male'].sum()), size = 12) 
    grid.axes[row, 1].text(60.2, 0.052, 'Survived n = '+str(titanic_data.loc[titanic_data['Pclass']==row+1].loc[titanic_data['is_child_def']==False].loc[titanic_data['Age'].isnull()==False].loc[titanic_data['Survived']==1]['is_female'].sum()), size = 12) 
    grid.axes[row, 0].text(60.2, 0.047, 'Perished n = '+str(titanic_data.loc[titanic_data['Pclass']==row+1].loc[titanic_data['is_child_def']==False].loc[titanic_data['Age'].isnull()==False].loc[titanic_data['Survived']==0]['is_male'].sum()), size = 12) 
    grid.axes[row, 1].text(60.2, 0.047, 'Perished n = '+str(titanic_data.loc[titanic_data['Pclass']==row+1].loc[titanic_data['is_child_def']==False].loc[titanic_data['Age'].isnull()==False].loc[titanic_data['Survived']==0]['is_female'].sum()), size = 12) 



grid.set_ylabels('Frequency density', size=12) 

# Squash down a little and add title to facetgrid  
plt.subplots_adjust(top=0.9) 
grid.fig.suptitle('Age distribution of adults by Pclass and Sex for Survived vs. Perished') 
+0

我花了一段时间来重现问题。你能否请下次问一个问题,产生一个可以直接复制和粘贴的[mcve]。您实际上并不需要这种复杂的数据框来问一个关于FacetGrid映射中色调的问题,对吧? – ImportanceOfBeingErnest

回答

2

kwargs的包含标签和相应的色调的颜色。因此,使用

def vertical_mean_line_survived(x, **kwargs): 
    ls = {"0":"-","1":"--"} 
    plt.axvline(x.mean(), linestyle =ls[kwargs.get("label","0")], 
       color = kwargs.get("color", "g")) 
    txkw = dict(size=12, color = kwargs.get("color", "g"), rotation=90) 
    tx = "mean: {:.2f}, std: {:.2f}".format(x.mean(),x.std()) 
    plt.text(x.mean()+1, 0.052, tx, **txkw) 

我们会得到

enter image description here

+0

非常感谢 - 这很好。并为过长的问题代码道歉 - 我是一个相对的stackoverflow新手。 在实际数据上,总体0,1的平均线非常接近,因此xmean()+ 1的对齐可以覆盖它们。其中提出了2个后续步骤: 1)如何通过色调参数改变文本位置参数? 2)是否有函数返回kde曲线的最大y值(所以我设置y的合作伙伴相对于那个? 非常感谢。 – chrisrb10

+0

1.你得到色调参数为'kwargs.get(“标签“),所以你可以做'如果kwargs.get(”label“)==”0“:... else:...'并为这两种情况设置不同的位置。2.问题是你会需要得到标记函数中kde曲线的y值,我想你可以重新计算它内部的kde曲线,例如使用[scipy.stats.gaussian_kde。](https://docs.scipy.org/doc/scipy -0.19.0/reference/generated/scipy.stats.gaussian_kde.html),然后取其最大值,但看起来似乎有点矫枉过正 – ImportanceOfBeingErnest

+0

Thanks。'kwargs.get('label')'完美地工作,同意重新计算标签位置的kde曲线是过度杀伤 - 现在太雄心勃勃了。 – chrisrb10

相关问题