python - 在彼此之上显示 2 个 Seaborn 图

标签 python plot seaborn

我想创建一个图表,在其中可视化单个数据点以及不同变量的一些集中趋势度量。 我想我可以在同一轴上使用 seaborn stripplot(有一些抖动)和 pointplot。

import seaborn as sns
tips = sns.load_dataset("tips")
sns.set(style="white", color_codes=True)
ax = sns.stripplot(x="sex", y="total_bill", hue='smoker', data=tips,
                   split=True, jitter=True)
ax = sns.pointplot(x="sex", y="total_bill", hue='smoker', data=tips,
                   dodge=True, join=False)

但是,当我这样做时,条形图中的数据值和点图中的误差条是倾斜的,并且没有显示在彼此之上: example Figure

我该如何解决这个问题,以便误差线显示在抖动的数据值之上?

最佳答案

pointplot 的 dodge 参数可以是任意值,不只是 True 或 False,它表示点之间的间隔。通过一些反复试验,您可以找到将这两个点放在条状图点正上方的值。我发现在这种情况下 0.4 可以解决问题。 也许有更优雅的解决方案,但这是我所知道的:)

代码如下:

import seaborn as sns
tips = sns.load_dataset("tips")
sns.set(style="white", color_codes=True)
fig, ax = sns.plt.subplots()
sns.pointplot(x="sex", y="total_bill", hue='smoker', data=tips,
                   dodge=0.4, join=False, ax=ax, split = True)
sns.stripplot(x="sex", y="total_bill", hue='smoker', data=tips,
                   split=True, jitter=True, ax = ax)

enter image description here

关于python - 在彼此之上显示 2 个 Seaborn 图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51360524/

相关文章:

python-3.x - sns.distplot 正在为唯一的正变量绘制负值

python - 如何通过将某些元素保留在末尾来对Python列表进行排序

python - 如何在多个Python版本中使用同一个源代码?

python - 使用 ChatGPT API 仅使用自定义知识而非一般知识来训练聊天机器人

python - Matplotlib 错误栏,倾覆不起作用?

python - 如何在 matplotlib 图中添加轴偏移?

python - Seaborn sns.set() 改变绘图背景颜色

python - 有效扩张线性回归

algorithm - 有效地绘制星图

r - 在一定范围R内绘制拟合线