【问题标题】:Connecting non-adjacent data points in Seaborn pointplot在 Seaborn 点图中连接不相邻的数据点
【发布时间】:2017-10-21 02:57:00
【问题描述】:

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

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

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

【问题讨论】:

  • 我建议不要使用 seaborn。只需在 matplotlib 中使用带有误差线的常用散点图或线图。

标签: python matplotlib seaborn


【解决方案1】:

我认为没有选项可以在缺少数据点的地方进行插值,因此该行改为停止。 This question 2016 年的同一主题仍未得到答复。

相反,您可以按照 cmets 中的建议使用 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)

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

【讨论】:

  • 非常感谢。我还有一个问题,在左边和中间的图像上,x轴上两个相邻值之间的差距应该是不同的,但是现在它们是相同的,我该如何改变呢?
  • @emmaqqq 不用担心,如果它解决了您的问题,请接受此答案(单击复选标记)。然后我可以关闭我链接到的问题作为此问题的副本。对于您的评论,请打开一个新问题并随时在此处链接。
  • @emmaqqq 我忘了说:对于您的新问题,请尝试包含一个示例数据集,以便其他人轻松重现您的问题并帮助您。 Seaborn 示例数据集是一个很好的资源,请随意重用我在此处回答的代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-22
  • 1970-01-01
  • 2021-01-13
  • 2015-03-04
  • 1970-01-01
  • 2018-11-09
  • 2014-12-08
相关资源
最近更新 更多