0

I have some trouble understanding seaborns FacetGrids. I have 16 cotegorical columns in my data and I want to use countplot to show the distribution over the values. In addition i have one target variable Y which have to classes. Now I want the calculations for the countplot to be class-dependet. So for each of my 16 input variables I want to have to subplots ( for class 0 and class 1). I can achieve this by (not sure if this is the best solution):

for col in df:
   g = sbn.FacetGrid(df, col="Y")
   g.map(sbn.countplot, col) 

But now I want these 16 plots to be in a 4x4 grid. I tried:

fig, axes =plt.subplots(4, 4, figsize=(18, 10))

for col, ax in zip(df, axes.flat):
  g = sbn.FacetGrid(df, col="Y")
  g.map(sbn.countplot, col, ax=ax) 

plt.tight_layout()
plt.show()

But this does not work. I Also tried:

  g = sbn.FacetGrid(df, col="Y")
  for col in df:
     g.map(sbn.countplot, col)

But only the last col is plotted. Can you help me?

Thanks!

f_3464gh
  • 162
  • 3
  • 11
  • Each facetgrid is its own figure and you cannot put figures into other figures, other than relying on [fragile hacks like this](https://stackoverflow.com/a/47664533/4124317). So ideally, you would either create one single FacetGrid, or not rely on seaborn and instead fill a normal matplotlib grid of axes with your data. Check [this question](https://stackoverflow.com/questions/44158276/combine-different-seaborn-facet-grids-into-single-plot/44159496#44159496). – ImportanceOfBeingErnest Mar 09 '20 at 14:53
  • how can i create one single facegrid to achieve what i want? – f_3464gh Mar 09 '20 at 15:07
  • I have no idea, because I don't understand your data structure. – ImportanceOfBeingErnest Mar 09 '20 at 15:14
  • My structure looks like this: one row looks like this [1, 4, 3, 2, 1] where the first four values are input features and the last value is the class index, so I want to predict the class index with the features. No for each input column I want to calculate the countplot but with respect to the class label which can be 0 or 1 – f_3464gh Mar 09 '20 at 15:19

0 Answers0