[Solved] 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 #1:

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

Respondent: Serenity
The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Leave a Reply

Your email address will not be published.