nerd
nerd

Reputation: 493

How to plot histogram subplots for each group

When I run the following code, I get 4 different histograms separated by groups. How can I achieve the same type of visualization with 4 different sns.distplot() also separated by their groups?

df = pd.DataFrame({
    "group": [1, 1, 2, 2, 3, 3, 4, 4],
    "similarity": [0.1, 0.2, 0.35, 0.6, 0.7, 0.25, 0.15, 0.55]
})

df['similarity'].hist(by=df['group'])

enter image description here

Upvotes: 2

Views: 5079

Answers (2)

Quang Hoang
Quang Hoang

Reputation: 150785

You can use FacetGrid from seaborn:

import seaborn as sns

g = sns.FacetGrid(data=df, col='group', col_wrap=2)
g.map(sns.histplot, 'similarity')

Output:

enter image description here

Upvotes: 2

Trenton McKinney
Trenton McKinney

Reputation: 62513

  • seaborn is a high-level api for matplotlib, and pandas uses matplotlib as the default plotting backend.
  • From seaborn v0.11.2, sns.distplot is deprecated, and, as per the Warning in the documentation, it is not recommended to directly use FacetGrid.
  • sns.distplot is replaced by the axes-level function sns.histplot, and the figure-level function sns.displot.
  • Also see seaborn histplot and displot output doesn't match
  • It is easy to produce a plot, but not necessarily to produce the correct plot, unless you are aware of the different parameter defaults for each api.
    • Note the difference between common_bins as True and Fales.
  • Tested in python 3.10, pandas 1.4.2, matplotlib 3.5.1, seaborn 0.11.2

common_bins=False

import seaborn as sns

# plot
g = sns.displot(data=df, x='similarity', col='group', col_wrap=2, common_bins=False, height=4)

enter image description here

common_bins=True (4)

  • sns.displot, and pandas.DataFrame.plot with kind='hist' and bins=4 produce the same plot.
g = sns.displot(data=df, x='similarity', col='group', col_wrap=2, common_bins=True, bins=4, height=4)

enter image description here

# reshape the dataframe to a wide format
dfp = df.pivot(columns='group', values='similarity')

axes = dfp.plot(kind='hist', subplots=True, layout=(2, 2), figsize=(9, 9), ec='k', bins=4, sharey=True)

enter image description here

Upvotes: 2

Related Questions