1

I am experiencing certain difficiulties plotting multiple histograms for unique values from a column and I couldn't find any topic on this so I appreciate your help in advance.

I have a table from which I need to take 2 columns: one with unique names of sports (Soccer, Volleyball, Hockey etc.) and another one with number of visits that represents number of people visited each type of sports in certain months. Apart from it there are much more columns in this table however the idea is to take only 2 of it and to plot multiple histograms using Seaborn.

Let's say it looks like this:

Sports - Visits

Soccer - 12300
Hockey - 7500
Volleyball - 3600
Hockey - 6800
Volleyball - 5300
Soccer - 9100
Hockey - 4800
etc.

The solution I found was not the best and considers converting my current table to pivot where names of sports are represented as features (columns) and visits are represented as values.

Then you can run something like this:

for i in enumerate(df.columns):
    plt.subplot(3, 7, i[0]+1)
    sns.histplot(task321[i[1]], bins=20)

and get this: enter image description here

Is there an easier way of doing this without making extra pivot tables with names as features?

  • I'm not sure I understand what output you're going for. Do you mean you want to plot all of those values in a single histogram where each bar is the value for a sport (so you'd get as many bars as there are sports)? – Adam Jaamour Sep 13 '22 at 09:23
  • Sorry if I didn't make it more clear. The issue is if you have a table of many features where one of them is the column with sports names and another one is the column with visits. Is there a way of plotting multiple histograms without making a new table where sports names are represented as features (columns names) but instead to work with the existing one and pass unique values of sports names to sns.histplot to plot multiple histograms? – Fitzpatrick Sep 13 '22 at 09:42

1 Answers1

3

I think what you want here is FacetGrid.map()

**Edit: Per Trenton's comments, we will use displot()

Documentation here

import pandas as pd
import seaborn as sns

cols = ['Sports','Visits']


data = [['Soccer',12300],
        ['Hockey',7500],
        ['Volleyball',3600],
        ['Hockey',6800],
        ['Volleyball',5300],
        ['Soccer',9100],
        ['Hockey',4800]]

df = pd.DataFrame(data, columns=cols)
  
#g = sns.FacetGrid(df, col="Sports")
#g.map(sns.histplot, "Visits")
#g.map(sns.histplot, "Visits", bins=20) #<-- or to set bins

sns.displot(data=df, x='Visits', col="Sports", col_wrap=4) #<-- set how many columns of graphs with col_wrap

Output:

enter image description here

chitown88
  • 27,527
  • 4
  • 30
  • 59
  • Yes, exactly. Thanks a lot! But now how can I make them look bigger and go in a few rows? They go in one line and there's 20 of them, you can barely see anything. – Fitzpatrick Sep 13 '22 at 10:03
  • @Fitzpatrick, I provided the link to the documentation. In the future, please first refer to that as it states it there. It says you can define how many columns of graphs. So let's say you have 20, define your cols as 4 and you get 5 rows of 4 cols. `g = sns.FacetGrid(df, col="Sports", col_wrap=4)`. Also, please accept the solution as it answers your question (and do the same with the question you posted back in December ;-). – chitown88 Sep 13 '22 at 11:22
  • 1
    In the current version of seaborn, it is not recommended to directly use `sns.FacetGrid` the correct method is `sns.displot`. – Trenton McKinney Sep 13 '22 at 20:33
  • @TrentonMcKinney, thanks. I was not aware of that. I made the edit in the solution. Thanks again. – chitown88 Sep 14 '22 at 11:31