Seaborn stripplot better way to visually separate variables

1.2k Views Asked by At

What is a way to visually separate the variables in a Seaborn stripplot? (Using Seaborn v. 0.11.1)

For example,

df = sns.load_dataset('iris')
dfm = pd.melt(df, id_vars=["species"])
sns.stripplot(data=dfm, x="species", y="value", hue="variable", dodge=True)
plt.legend(bbox_to_anchor=(1.05, 1), loc=2)

enter image description here

Is there a way to box-out the different species? My real dataset is busier and getting hard to read. I've increased the figure size, but I still want some sort of subtle visual separator.

Here's a sketch of generally what I am after (open to more visually appealing solutions):

enter image description here

2

There are 2 best solutions below

0
On BEST ANSWER

If you want a line, something like this might work for you. Line style can be changed

import seaborn as sns

df = sns.load_dataset('iris')
dfm = pd.melt(df, id_vars=["species"])
sns.stripplot(data=dfm, x="species", y="value", hue="variable", dodge=True)
plt.legend(bbox_to_anchor=(1.05, 1), loc=2)

for x in range(0, len(dfm['species'].unique()) - 1):
  plt.plot([x + 0.5, x + 0.5], [0, dfm['value'].max()], c='black')

enter image description here

Something like this could also be good

import seaborn as sns

df = sns.load_dataset('iris')
dfm = pd.melt(df, id_vars=["species"])
sns.stripplot(data=dfm, x="species", y="value", hue="variable", dodge=True)
plt.legend(bbox_to_anchor=(1.05, 1), loc=2)

for x in range(0, len(dfm['species'].unique())):
    plt.axvspan(x - 0.5, x + 0.5, facecolor='black', alpha=[0.2 if x%2 == 0 else 0.1][0])

enter image description here

0
On

You could try the figure-level variant of sns.stripplot: sns.catplot(kind='strip', ...). This creates a separate subplot per species:

import seaborn as sns
import pandas as pd

df = sns.load_dataset('iris')
dfm = pd.melt(df, id_vars=["species"])
g = sns.catplot(data=dfm, col="species", y="value", x="variable", hue="variable", kind='strip')
g.add_legend()
g.set(xlabel='') # remove the x labels
g.set(xticks=[]) # remove the xticks (these contain the same info as the legend)

sns.catplot with kind='strip'