Search code examples
pythonpandasmatplotlibseabornswarmplot

seaborn pointplot above swarmplot


I try to plot group wise median values using seaborn's pointlot on top of a swarmplot. Even though I call pointPlot second, the point plot ends up behind the swarmplot. How can I change the 'layer order' such that the point plot is in front of the swarmplot?

datDf=pd.DataFrame({'values':np.random.randint(0,100,100)})
datDf['group']=np.random.randint(0,5,100)
sns.swarmplot(data=datDf,x='group',y='values')
sns.pointplot(data=datDf,x='group',y='values',estimator=np.median,join=False)

enter image description here


Solution

  • Use zorder property to set proper drawing order.

    import numpy as np
    import pandas as pd
    import seaborn as sns
    import matplotlib.pylab as plt
    
    datDf=pd.DataFrame({'values':np.random.randint(0,100,100)})
    datDf['group']=np.random.randint(0,5,100)
    sns.swarmplot(data=datDf,x='group',y='values',zorder=1)
    sns.pointplot(data=datDf,x='group',y='values',estimator=np.median,join=False, zorder=100)
    plt.show()
    

    enter image description here