Ilkay Isik
Ilkay Isik

Reputation: 213

Display 2 Seaborn plots on top of each other

I would like to create a plot where I visualize single data points and also some central tendency measure for different variables. I thought I can use seaborn stripplot (with some jitter) and pointplot on the same axis.

import seaborn as sns
tips = sns.load_dataset("tips")
sns.set(style="white", color_codes=True)
ax = sns.stripplot(x="sex", y="total_bill", hue='smoker', data=tips,
                   split=True, jitter=True)
ax = sns.pointplot(x="sex", y="total_bill", hue='smoker', data=tips,
                   dodge=True, join=False)

However, when I do this the data values from the strip plot and the error bars from the point plot are skewed and not displayed on top of each other: example Figure

How can I solve this issue so that the error bars are displayed on top of the jittered data values?

Upvotes: 6

Views: 7753

Answers (1)

BossaNova
BossaNova

Reputation: 1527

The dodge parameter of the pointplot can be of any value, not just True or False, and it indicates the separation between the points. With a little bit of trial and error you can find the value that places the two dots right on top of the stripplot dots. I found that 0.4 in this case does the trick. Maybe there's a more elegant solution, but this is the one I know :)

Here is the code:

import seaborn as sns
tips = sns.load_dataset("tips")
sns.set(style="white", color_codes=True)
fig, ax = sns.plt.subplots()
sns.pointplot(x="sex", y="total_bill", hue='smoker', data=tips,
                   dodge=0.4, join=False, ax=ax, split = True)
sns.stripplot(x="sex", y="total_bill", hue='smoker', data=tips,
                   split=True, jitter=True, ax = ax)

enter image description here

Upvotes: 5

Related Questions