2

Is there a way to align the mean+SEM of a pointplot and the corresponding swarmplot?

Here is my code:

import seaborn as sns
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

fig, ax = plt.subplots(nrows=1, ncols=1)
n=200
to_plot = np.random.uniform(low=0.0, high=1.0, size=n)
mods = ['a','b']
model_col = mods*(n/2)
opt=['1']*(n/2)+['2']*(n/2)

d={'Model':pd.Series(model_col),'Par':pd.Series(to_plot),'opt':pd.Series(opt)}
df = pd.DataFrame(d)
sns.swarmplot(x='Model', y='Par',hue='opt',dodge=True,data=df,size=2,palette=['#469990','#000075'])
sns.pointplot(x="Model", y="Par", hue='opt', data=df,join=False,dodge=True,
     ci=68,n_boot=1000,capsize=0.1,errwidth=0.5,scale = 1.5,palette=['k','k'])

ax.get_legend().remove()
plt.show()

Here is my plot:

enter image description here

Marcus Campbell
  • 2,746
  • 4
  • 22
  • 36
Ale
  • 133
  • 7

1 Answers1

4

It seems swarmplot and pointplot use different defaults for their respective dodge paramter. However you may set them to equal values, e.g.

sns.swarmplot(...,  dodge=0.4) 
sns.pointplot(...,  dodge=0.4) 
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
  • 2
    the dodge parameter for swarmplot is purely boolean. Passing a float value does not impact the plot behavior. Curiously, it does not raise an error to pass a float, instead any value different than "False", or "0" is interpreted as True. See https://seaborn.pydata.org/generated/seaborn.swarmplot.html Note that unlike for swarmplot, the pointplot dodge parameter *does* accept a boolean *or* a float value. – S.A. Oct 21 '20 at 10:02