0

Is there any chance to use subplots to show 2 stacked bar charts made with pandas crosstab ?
I'm not able to show 2 charts side by side in the same figure.

my code:

cross = pd.crosstab(index=base['year'], columns=base['categ'], values=base['price'], aggfunc='sum')
cross2 = pd.crosstab(index=base['categ'], columns=base['year'], values=base['price'], aggfunc='sum')

fig, (ax1, ax2) = plt.subplots(1,2, figsize = (10,6))
ax1 = cross.plot(kind='bar', stacked=True, rot=0)
ax2 = cross2.plot(kind='bar', stacked=True, rot=0)
plt.show()

I'm always uncomfortable when i have to use charts with a pandas crosstab because the only solution I know is crosstab.plot() not very efficient to manage data.
Is pandas.plot() is the only solution to plot a chart from a crosstab ?
Or any other way to plot revenue / year (hue category) or revenue / category (hue year) ?

I've tried seaborn without crosstab but not way to show stacked bar.
Also impossible to use 'hue' or an equivalent with matplotlib (to the best of my knowledge...)

Thanks for your help !

Lilly_Co
  • 169
  • 12
  • 1
    You need `cross.plot(..., ax=ax1)` and `cross2.plot(..., ax=ax2)` – JohanC Jan 13 '22 at 23:50
  • `sns.barplot(data=base, x='year', y='revenue', hue='categ', estimator=sum, ci=None )` would be seaborn's way. But no stacking, bars are dodged. – JohanC Jan 14 '22 at 00:05

0 Answers0