pythonmatplotlibseaborncatplotstripplot

Seaborn stripplot better way to visually separate variables


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


Solution

  • 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