Python 在Seaborn pointplot中更改x轴上两个相邻值之间的间距
当我使用Seaborn pointplot进行绘图时,x轴上两个相邻值之间的间距应该不同,但现在所有值都相同,我如何更改 例如,5和7之间的间隙为2,其他间隙为1,但在绘图中它们的长度都相同Python 在Seaborn pointplot中更改x轴上两个相邻值之间的间距,python,seaborn,Python,Seaborn,当我使用Seaborn pointplot进行绘图时,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=Tru
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)
这在Seaborn中是不可能的,原因似乎是它会干扰
hue=
参数中的嵌套分组,如中所示,该参数过去有一个positions=
关键字(如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)
在点图中,X轴是一个分类轴,想象它更像[“苹果”、“香蕉”、“樱桃”,因此没有“距离”。相反,您可能希望使用matplotlib散点图或errorbar图。