-1

I have this dataset table And i want to plot profit made by different sub_category in different region.

now i am using this code to make a plot using seaborn

sns.barplot(data=sub_category_profit,x="sub_category",y="profit",hue="region")

I am getting a extreamly huge plot like this output is there is any way i can get sub-plots of this like a facet-gird. Like subplots of different sub_category. I have used the facet grid function but it is the also not working properly.

g=sns.FacetGrid(data=sub_category_profit,col="sub_category")
g.map(sns.barplot(data=sub_category_profit,x="region",y="profit"))

I am getting the following output

As you can see in the facet grid output the plots are very small and the bar graph is just present on one grid.

  • As per the documentation, it is not recommended to directly use [`FacetGrid`](https://seaborn.pydata.org/generated/seaborn.FacetGrid.html). Instead, use [`sns.catplot`](https://seaborn.pydata.org/generated/seaborn.catplot.html) with `kind='bar'`. `g = sns.catplot(kind='bar', data=sub_category_profit, x="region", y="profit", col="sub_category", col_wrap=3)` – Trenton McKinney Oct 22 '22 at 18:51

1 Answers1

-1

See docs on seaborn.FacetGrid, particularly the posted example, where you should not pass the data again in the map call but simply the plot function and x and y variables to draw plots to corresponding facets.

Also, consider the col_wrap argument since you do not specify row to avoid the very wide plot output.

g=sns.FacetGrid(data=sub_category_profit, col="sub_category", col_wrap=4)
g.map_dataframe(sns.barplot, x="region", y="profit")
Parfait
  • 104,375
  • 17
  • 94
  • 125
  • It is not working without passing the data again in map. It says "Could not interpret input 'region' ". And when i am passing the data into it i am getting same plot on every grid. – bharat bisht Oct 22 '22 at 13:41
  • Interesting. Try using [`map_dataframe`](https://seaborn.pydata.org/generated/seaborn.FacetGrid.map_dataframe.html) in place of `map`. Alternatively, do not use named args `x` and `y`. – Parfait Oct 22 '22 at 14:09
  • thank you so much it worked this time with map_dataframe. But i am not getting name of points on X-axis on every gird its just on the gird of the last row. Is there is any way to solve that. And again thanks for your help. – bharat bisht Oct 22 '22 at 14:45
  • Pass `sharex=False` and `sharey=False` in `FacetGrid` – Parfait Oct 22 '22 at 15:17
  • wow thank you so much the plot looks really amazing now. – bharat bisht Oct 22 '22 at 15:43