pandasseabornfacet-gridgrouped-bar-chartcatplot

Using seaborn.catplot with Row Faceting for Yearly Data Comparison


I've been working with seaborn.catplot in order to have a bar plot (data sample bellow) adding up the values in a counts column for a set of reasons, separated by a group of companies:

sns.catplot(x='Bill_Name', y='counts', hue='Reason', 
            data=data, kind='bar', height=6, aspect=13/6, 
            legend=True, palette='hls')

enter image description here

Now for each value there's also a column year. So I was thinking of using seaborn.FacetGrid, in order to have the above in a grid of rows.

So if I understood the way this works correctly, sns.FacetGrid must be fed the data and the year column in this case as the row argument, and then use sns.map, with sns.catplot and its corresponding parameters, however this is not working properly:

g = sns.FacetGrid(data, row="year", height=4, aspect=.5)
g = g.map(sns.catplot, x='Bill_Name', y='counts', hue='Reason', 
            data=data, kind='bar', height=6, aspect=13/6, 
            legend=True, palette='hls')

What am I doing wrong?


Here's a sample of the data:

   Bill_Name      year   Reason  counts
0   CompanyC     2018.0  Reason6       2
1   CompanyC     2017.0  Reason5       8
2   CompanyB     2017.0  Reason3     146
3   CompanyC     2015.0  Reason6       2
4   CompanyC     2017.0  Reason1    1828
5   CompanyC     2016.0  Reason3     237
6   CompanyB     2018.0  Reason4    1097
7   CompanyC     2016.0  Reason4      11
8   CompanyB     2016.0  Reason5      12
9   CompanyC     2017.0  Reason2     834
10  CompanyB     2016.0  Reason3      97
11  CompanyC     2017.0  Reason6     714
12  CompanyA     2017.0  Reason1    4288
13  CompanyA     2016.0  Reason2    2444
14  CompanyC     2017.0  Reason3     293
15  CompanyB     2016.0  Reason1    1576
16  CompanyA     2016.0  Reason4      37
17  CompanyA     2018.0  Reason5       1
18  CompanyC     2018.0  Reason1     908
19  CompanyC     2018.0  Reason2     478
20  CompanyA     2015.0  Reason1    3826
21  CompanyB     2016.0  Reason4     119
22  CompanyB     2017.0  Reason2    1404
23  CompanyC     2016.0  Reason1    1884
24  CompanyC     2015.0  Reason4       1
25  CompanyA     2016.0  Reason1    6360
26  CompanyA     2018.0  Reason3     225
27  CompanyA     2018.0  Reason4      63
28  CompanyC     2018.0  Reason4     162
29  CompanyC     2016.0  Reason2    1504

Solution

  • You can avoid the facetgrid entirely if you add the row='year' argument to the seaborn catplot:

    sns.catplot(x='Bill_Name', y='counts', hue='Reason',row='year', data=data, kind='bar', height=6, aspect=13/6, legend=True, palette='hls')