当我使用Seaborn点图进行绘图时,x轴上两个相邻值之间的差距应该不同,但现在所有这些值都相同,我怎么能改变它?
例如,5和7之间的间隙是2,其他间隙是1,但在图中它们都具有相同的长度。
import seaborn as sns
tips = sns.load_dataset('tips')
tips.loc[(tips['size'] == 6), 'size'] = 7
sns.pointplot('size', 'total_bill', 'sex', tips, dodge=True)
答案 0 :(得分:0)
这在Seaborn中是不可能的,原因似乎是它会干扰hue=
参数的嵌套分组,如changelog describing why this functionality was removed from the sns.violinplot
所示,曾经positions=
1}}关键字(与plt.boxplot
一样)。
如评论中所示,可以使用plt.errorbar
来实现预期结果。
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
tips = sns.load_dataset('tips')
tips.loc[(tips['size'] == 6), 'size'] = 7
# This is to avoid that the points from the different curves are in the
# exact same x-position (so that the error bars to not obfuscate each other)
offset_amount = 0.1
gender_groups = tips.groupby('sex')
num_groups = gender_groups.ngroups
lims = num_groups * offset_amount / 2
offsets = np.linspace(-lims, lims, num_groups)
# Calculate and plot the mean and error estimates.
fig, ax = plt.subplots()
for offset, (gender_name, gender) in zip(offsets, gender_groups):
means = gender.groupby('size')['total_bill'].mean()
errs = gender.groupby('size')['total_bill'].sem() * 1.96 #95% CI
ax.errorbar(means.index-offset, means, marker='o', yerr=errs, lw=2)