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?