【问题标题】:Display 2 Seaborn plots on top of each other显示 2 个 Seaborn 地块彼此叠加
【发布时间】:2018-07-16 11:15:48
【问题描述】:

我想创建一个图表,在其中可视化单个数据点以及不同变量的一些集中趋势度量。 我想我可以在同一轴上使用 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)

但是,当我这样做时,条形图中的数据值和点图中的误差线是倾斜的,并且不会彼此重叠显示:

如何解决此问题,以便误差条显示在抖动数据值的顶部?

【问题讨论】:

    标签: python plot seaborn


    【解决方案1】:

    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)
    

    【讨论】:

    • 您可以对两个绘图使用相同的dodgesns.stripplot(..., dodge=0.3)sns.pointplot(..., dodge=0.3)。这消除了“试错”组件。
    • @ImportanceOfBeingErnest 不错的提示
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-20
    • 2019-08-12
    相关资源
    最近更新 更多