Say I have data that I want to box plot and overlay with a swarm plot in seaborn, whose colors of the points add additional information on the data.
Question: How can I get box plots to be close to each other for a given x axis value (as is done in hue) without refactorizing x to the hue value and the x axis value?
For example, here I want to overlay the points to the box plot and want the points further colored by ‘sex’. Example:
plt.figure(figsize = (5, 5))
sns.boxplot(x = 'class', y = 'age',
hue = 'embarked', dodge = True, data = df)
sns.swarmplot(x = 'class', y = 'age',
dodge = True,
color = '0.25',
hue = 'sex', data = df)
plt.legend(bbox_to_anchor = (1.5, 1))
EDIT:
The idea would be to have something that looks like the 'S' box for 'Third' in the plot (I made a fake example in powerpoint, so hue in both boxplot and swarmplot are the same to overlay the points on the appropriate boxes).
Is there a way to make this plot without first refactorizing the x-axis to ‘first-S’, ‘first-C’, ‘first-Q’, ‘second-S’, etc and then add hue by ’sex’ in both plots?

Using original
xascolandhueasxTo work with two types of
hue, seaborn's alternative is to create aFacetGrid. The originalx=then becomes thecol=(or therow=), and one of the hues becomes the newx=.Here is an example. Note that
aspect=controls the width of the individual subplots (the width beingheight*aspect).Only using
huefor theswarmplot, without dodgeHere is a variant, where the
boxplotdoesn't usehue, but theswarmplotdoes. A bit more padding can be added inside the subplots, and the boxplots can be made touching viawidth=1. Suppressing the outliers of theboxplotlooks cleaner, as they would overlap with the outlier of theswarmplot.