在Seaborn pointplot中连接非相邻数据点

时间:2017-10-17 17:05:49

标签: python matplotlib seaborn

我想用Seaborn点图绘制分类图,但不相邻的数据点不与图中的线连接。我想在非相邻点之间插值,并以与相邻点连接相同的方式连接它们,我该怎么做?

一个例子:在左图和中图中,蓝点和绿点应分别用曲线连接,但现在它们被分成小部分。如何绘制左图和中图像就像右图一样?

enter image description here

fig, axs = plt.subplots(ncols=3, figsize=(10,5))
exp_methods = ['fMRI left', 'fMRI right', 'MEG']
for i in range(3):
    experiment = exp_methods[i]
    dataf = df[df['data']==experiment]    
    sns.pointplot(x='number_of_subjects', y='accuracy', hue='training_size', data=dataf,
               capsize=0.2, size=6, aspect=0.75, ci=95, legend=False, ax=axs[i])

1 个答案:

答案 0 :(得分:1)

我认为没有选项可以在缺少数据点的位置进行插值,因此线路会停止。关于2016年同一主题的This question仍未得到答复。

相反,您可以按照评论中的建议使用plt.errorbar,或者之后使用plt.plot添加行,同时仍然使用seaborn绘制平均值和误差线:

import seaborn as sns

tips = sns.load_dataset('tips')

# Create a gap in the data and plot it
tips.loc[(tips['size'] == 4) & (tips['sex'] == 'Male'), 'size'] = 5
sns.pointplot('size', 'total_bill', 'sex', tips, dodge=True)

enter image description here

# Fill gap with manual line plot
ax = sns.pointplot('size', 'total_bill', 'sex', tips, dodge=True, join=False)

# Loop over the collections of point in the axes and the grouped data frame
for points, (gender_name, gender_slice) in zip(ax.collections, tips.groupby('sex')):
    # Retrieve the x axis positions for the points
    x_coords = [coord[0] for coord in points.get_offsets()]
    # Manually calculate the mean y-values to use with the line
    means = gender_slice.groupby(['size']).mean()['total_bill']
    ax.plot(x_coords, means, lw=2)

enter image description here