0

I'm having an issue getting my boxplot to align with my x axis labels. I've tried adjusting the size of the chart, but the data points still look a little off. I appreciate any help!

This is the current chart:

Current Chart

Kingsley
  • 14,398
  • 5
  • 31
  • 53
  • Closed as a duplicate because the `dodge=False` answer in the duplicate is the older answer (2019-11-28). The accepted answer here, doesn't explain how to resolve the issue. – Trenton McKinney Jun 01 '23 at 17:12

2 Answers2

3

I'm guessing it's because you're using two categorical variables; x, and hue. This creates a so called "nested" (search for the key-word "smoke") box-plot, and if one of the categories is empty in some sense, might cause the observed off-set.

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
ShlomiF
  • 2,686
  • 1
  • 14
  • 19
2

This misalignment can happen when the hue argument is set.

You can add the dodge=False argument to the sns.boxplot function to keep boxplots aligned with the x-axis labels.

In your example, it would look like this:

sns.boxplot(x=df["Groups"], y=df["Rate per Month"], hue=df["Hours per Month"], dodge=False)

Description of the dodge parameter from the the seaborn.boxplot documentation:

dodge: bool, optional

When hue nesting is used, elements should be shifted along the categorical axis.

Example from the seaborn.boxplot documentation.

njmarko
  • 46
  • 1
  • 4