Plot seaborn catplots for multiple columns

Viewed 7316

I have a dataframe with 93 features, and 9 class labels. I want to plot the values for each feature, with the respective class label, however, I want to produce a subplot of 93 plots each plot representing one feature in the dataset. I can produce one plot, it looks like this:

sns.catplot(x="feat_1", y="target", data=train)

enter image description here

Now I basically want to repeat the same thing, but 93 times in the form of a facet grid. I tried creating a subplot with 5 columns and 19 rows, then looping through the axes but failed miserably ... Thanks for the help, my data looks like this (93 feature columns and one target column):

    feat_1  feat_2  feat_3  feat_4  feat_5  feat_6  feat_7  feat_8  feat_9  feat_10 ... feat_85 feat_86 feat_87 feat_88 feat_89 feat_90 feat_91 feat_92 feat_93 target
id                                                                                  
32518   0   0   0   1   0   0   0   0   0   0   ... 0   0   0   0   0   0   0   0   0   Class_6
31734   0   1   7   5   0   0   0   0   0   1   ... 0   0   0   1   2   0   1   4   0   Class_6
57027   0   0   0   0   0   0   0   2   0   0   ... 0   0   0   0   0   0   1   0   0   Class_9
31629   0   1   0   0   0   0   0   1   1   0   ... 0   0   0   1   2   0   0   0   0   Class_6
14216   2   0   0   0   0   0   0   0   0   0   ... 0   0   0   1   0   0   0   0   0   Class_2
17376   0   0   0   0   0   0   0   0   0   0   ... 0   2   0   1   0   0   0   0   0   Class_2
10520   1   0   0   0   0   0   0   0   0   0   ... 0   3   0   0   0   0   0   0   0   Class_2
7665    0   0   0   0   0   0   0   0   0   0   ... 0   2   0   3   0   0   0   0   0   Class_2
26692   0   0   0   0   0   0   0   0   0   0   ... 4   0   0   0   0   0   0   0   0   Class_4
36809   0   0   3   4   0   0   0   0   0   0   ... 0   0   0   0   0   0   0   1   0   Class_6
47959   0   1   0   3   0   2   1   0   0   1   ... 6   0   0   0   1   1   0   0   1   Class_7
22649   0   0   0   0   1   0   0   0   0   1   ... 21  0   1   0   0   2   0   0   0   Class_3
34550   0   0   1   2   0   0   1   0   0   0   ... 0   0   1   0   0   1   1   1   1   Class_6
39943   3   0   0   0   0   0   0   0   0   0   ... 0   0   2   0   0   0   0   0   0   Class_6
38900   1   0   6   14  0   0   1   0   0   0   ... 0   0   1   0   0   0   0   0   0   Class_6
26333   0   0   1   0   0   0   1   1   0   0   ... 0   0   1   1   0   0   0   0   0   Class_4
16126   0   0   0   0   0   0   0   0   0   0   ... 0   0   1   10  0   0   0   0   0   Class_2
10490   0   0   0   0   0   0   0   1   0   0   ... 0   0   0   0   0   0   0   3   0   Class_2
58603   0   0   0   0   0   0   0   1   0   0   ... 0   0   0   0   0   0   28  0   1   Class_9
52668   0   0   1   2   0   0   0   4   0   0   ... 0   0   0   0   4   0   0   0   0   Class_8
1 Answers

To leverage the use of seaborn's FacetGrid (which is used by catplot) you need to transform you dataframe from "wide" to "long"

# dummy dataframe
N=20
N_features = 10
N_classes = 5
df = pd.DataFrame({f'feat_{i+1}': np.random.random(size=(N,)) for i in range(N_features)})
df['target'] = np.random.choice([f'Class_{i+1}' for i in range(N_classes)], size=(N,))

# transform from wide to long, then plot using the column 'features' to facet
df2 = df.melt(id_vars=['target'], var_name='features')
sns.catplot(data=df2, x='value', y='target', col='features', col_wrap=5, height=3, aspect=0.5)

enter image description here

Related