Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Separate plots with sns.distplot() grouped by a specific column's values

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading