1

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

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
  • 1
    Technically, this is a countplot (barplot) implemented with `sns.histplot`. A histogram shows the distribution of continuous data. More simply `ax = sns.countplot(data=tips, x='day', hue='sex')` – Trenton McKinney Apr 10 '23 at 18:58
  • 1
    See [how to get spacing between grouped bar plot](https://stackoverflow.com/q/66913686/7758804), [Bar graph spacing between grouped bars](https://stackoverflow.com/q/63378978/7758804), [How to increase the bar width in a grouped bar plot without reducing the gap between groups](https://stackoverflow.com/q/61183826/7758804) – Trenton McKinney Apr 10 '23 at 19:19
  • 1
    `ax = sns.histplot(data=tips, x="day", hue="sex", multiple="dodge", shrink=.9, ec='w', lw=2)`. – Trenton McKinney Apr 10 '23 at 19:22

1 Answers1

4

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

Oliver Lopez
  • 220
  • 1
  • 8