Reputation: 3396
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)
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):
Upvotes: 1
Views: 1898
Reputation: 80329
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)
Upvotes: 2
Reputation: 510
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')
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])
Upvotes: 2