Search code examples
pythonmatplotlibseaborngrouped-bar-chartseaborn-objects

How can I add space between bars in a dodged (hue) group?


How can I add space between Male and Female bars in seaborn histplot ?

import seaborn as sns
tips = sns.load_dataset("tips")
sns.histplot(data=tips, x="day", hue="sex", multiple="dodge", shrink=.9)
plt.show()

enter image description here


Solution

  • While I don't think this can be achieved directly with sns.histplot, you could use the seaborn.objects interface, which is more flexible (at the expense of complexity).

    For this specific example, here's how you could achieve the same plot, but with a gap between the bars (see seaborn.objects.Dodge):

    import seaborn as sns
    import seaborn.objects as so
    tips = sns.load_dataset("tips")
    so.Plot(tips, x="day", color="sex").add(so.Bar(), so.Count(), so.Dodge(gap=0.2))
    

    Plot using so