Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Distribution probabilities for each column data frame, in one plot

I am creating probability distributions for each column of my data frame by distplot from seaborn library sns.distplot(). For one plot I do

x = df['A']
sns.distplot(x);

I am trying to use the FacetGrid & Map to have all plots for each columns at once in this way. But doesn't work at all.

  g = sns.FacetGrid(df, col = 'A','B','C','D','E')
  g.map(sns.distplot())
like image 539
Annalix Avatar asked Dec 11 '22 06:12

Annalix


1 Answers

I think you need to use melt to reshape your dataframe to long format, see this MVCE:

df = pd.DataFrame(np.random.random((100,5)), columns = list('ABCDE'))
dfm = df.melt(var_name='columns')
g = sns.FacetGrid(dfm, col='columns')
g = (g.map(sns.distplot, 'value'))

Output: enter image description here


From seaborn 0.11.2 it is not recommended to use FacetGrid directly. Instead, use sns.displot for figure-level plots.

np.random.seed(2022)
df = pd.DataFrame(np.random.random((100,5)), columns = list('ABCDE'))
dfm = df.melt(var_name='columns')

g = sns.displot(data=dfm, x='value', col='columns', col_wrap=3, common_norm=False, kde=True, stat='density')

enter image description here

like image 155
Scott Boston Avatar answered Dec 28 '22 23:12

Scott Boston