带状图和线形图怪异结果

时间:2018-12-31 10:55:47

标签: python seaborn

当我使用lineplot或stripplot时,效果很好。但是同时使用中位数会发生变化;我不明白为什么!谢谢您的帮助。

sns.lineplot(x='quality', y='alcohol', data=df, estimator=np.median, err_style=None)
sns.stripplot(x='quality', y='alcohol', data=df, jitter=True, color='red', alpha=0.2, edgecolor='none')

stripplot

lineplot+stripplot

lineplot

1 个答案:

答案 0 :(得分:0)

这里发生的是,您的第一个图正在创建一个范围从0到n的x轴,并用从3到n的整数列表重新标记这些x刻度,然后在第二个图或带状图绘制在该x轴使用的是原始数字,因此此新图表的xtick 3从标记为xtick 6开始。

一种纠正此问题的方法是创建一个具有预定义范围的x轴,然后以该预定义比例绘制两个图表,请参见以下示例:

import seaborn as sns
import matplotlib.pyplot as plt
x = [3,4,5,6,7,8]
y = [10, 12, 15, 18, 19, 26]

#First axes creates the error in graphing
fig, ax = plt.subplots(1,2)
sns.lineplot(x=x,y=y, ax=ax[0])
sns.stripplot(x=x, y=y, ax=ax[0])

#Second axes shows correction
xplot = range(len(x))
sns.lineplot(x=xplot,y=y, ax=ax[1])
sns.stripplot(x, y=y, ax=ax[1])

输出:

enter image description here

相关问题