gpt4 book ai didi

python - 更改 Seaborn 点图中 x 轴上两个相邻值之间的间隙

转载 作者:行者123 更新时间:2023-12-04 17:48:39 24 4
gpt4 key购买 nike

当我用 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=True)

enter image description here

最佳答案

这在 Seaborn 中是不可能的,原因似乎是它会干扰 hue= 参数的嵌套分组,如 changelog describing why this functionality was removed from the sns.violinplot 中所示,它曾经有一个 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)

enter image description here

关于python - 更改 Seaborn 点图中 x 轴上两个相邻值之间的间隙,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46906052/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com