本文介绍了在 Seaborn 点图中连接非相邻数据点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

一个例子:在左边和中间的图像中,蓝点和绿点应该分别用曲线连接,但现在它们被分成了小部分.我怎样才能像右边一样绘制左边和中间的图像?

  fig,axs = plt.subplots(ncols = 3,figsize =(10,5))exp_methods = ['fMRI 左','fMRI 右','MEG']对于范围内的 i (3):实验= 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])
解决方案

我不认为可以在缺少数据点的位置进行插值,因此该行会停止.关于2016年同一主题的

 #用手动线条图填充间隙ax = sns.pointplot('size','total_bill','sex',技巧,闪避=真,join = False)#循环遍历轴和分组数据框中的点集合对于点,(性别名称,性别切片)在 zip(ax.collections,tips.groupby('sex')):# 检索点的 x 轴位置x_coords = [coord[0] 用于 points.get_offsets()] 中的坐标# 手动计算要与线一起使用的平均 y 值意思是=gender_slice.groupby(['size']).mean()['total_bill']ax.plot(x_coords,means,lw=2)

I want to plot categorical plots with the Seaborn pointplot, but data points that are not adjacent are not connected with a line in the plot. I would like to interpolate between non adjacent points, and connect them in the same way as adjacent points are connected, how can I do this?

An example: In the left and middle images, the blue and green points should be connected with a curve, respectively, but now they are separated into small parts. How can I plot the left and middle images just like the right one?

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])
解决方案

I don't think there is an option to interpolate where there are missing data points, and hence the line stops instead. This question on the same topic from 2016 remains unanswered.

Instead, you could use plt.errorbar as suggested in the comments, or add the lines afterwards using plt.plot while still using seaborn to plot the means and error bars:

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

这篇关于在 Seaborn 点图中连接非相邻数据点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 07:20