用线连接抖动的数据点 - seaborn python

Connecting jittered data points with lines - seaborn python

有没有办法提取下面代码生成的中间图中抖动点的 x 轴值?

查看问题here

# import libraries
import seaborn as sns
import matplotlib.pyplot as plt

# Create plot
ax2 = fig.add_subplot(132)
sns.stripplot(x="variable", y="value", data=pupil_long_df, dodge=True, jitter=True, alpha=.40, zorder=1, size=8, linewidth = 1)
sns.pointplot(x='variable', y='value', ci=95,data=pupil_long_df, join=False, scale=1, zorder=100, color='black', capsize = 0.05, palette = 'Paired') 

# Add lines between the points
lines3 = plt.plot([df.iloc[:,0], df.iloc[:,1]], color = 'grey', linewidth = 0.5, linestyle = '--')


我认为提取 stripplot 的 x 值是非常不切实际的......我的标准建议是,如果你想做的不仅仅是 seaborn 提供的标准图,那么通常更容易手工重新创建它们。请看下面的代码:

N=20
# dummy dataset
data = np.random.normal(size=(N,))
df = pd.DataFrame({'condition 1': data,
                   'condition 2': data+1,
                   'condition 3': data,
                   'condition 4': data-1})

jitter = 0.05
df_x_jitter = pd.DataFrame(np.random.normal(loc=0, scale=jitter, size=df.values.shape), columns=df.columns)
df_x_jitter += np.arange(len(df.columns))

fig, ax = plt.subplots()
for col in df:
    ax.plot(df_x_jitter[col], df[col], 'o', alpha=.40, zorder=1, ms=8, mew=1)
ax.set_xticks(range(len(df.columns)))
ax.set_xticklabels(df.columns)
ax.set_xlim(-0.5,len(df.columns)-0.5)

for idx in df.index:
    ax.plot(df_x_jitter.loc[idx,['condition 1','condition 2']], df.loc[idx,['condition 1','condition 2']], color = 'grey', linewidth = 0.5, linestyle = '--', zorder=-1)
    ax.plot(df_x_jitter.loc[idx,['condition 3','condition 4']], df.loc[idx,['condition 3','condition 4']], color = 'grey', linewidth = 0.5, linestyle = '--', zorder=-1)