0

I want to make a chart after applying groupby

So I have applied

Sales_comparison = SalesData[['Region', 'Sales2015', 
'Sales2016']].groupby(['Region']).agg(['sum'])

I have tried for the graph

ax = Sales_comparison[['Sales2015','Sales2016']].plot(kind='bar', title 
="Sales by region comparison", figsize=(7.5, 5), legend=True, fontsize=12)
ax.set_xlabel("Region", fontsize=12)
ax.set_ylabel("Sales", fontsize=12)
x = Sales_comparison.Region.index.tolist()
x_pos = [i for i, _ in enumerate(x)]
plt.xticks(x_pos, x)
plt.show()

But it is of no use

Is there any easier and shorter way to do what I want to achieve?

enter image description here

The data can be found in Link for the data

DavidG
  • 24,279
  • 14
  • 89
  • 82
  • 1
    The df.plot(kind='bar', stacked=True) should work for you. [This answer should help you further along.](https://stackoverflow.com/questions/23415500/pandas-plotting-a-stacked-bar-chart) – Max Oct 02 '18 at 08:49
  • So there is no need for indexation. If yes, then how to do so? – Abir Basak Oct 02 '18 at 10:03

1 Answers1

0

Could you elaborate on what you mean by indexation? I assume you mean labeling in the graph, which would work in a similar way that you have already done.

Sales_comparison = df[['Region', 'Sales2015', 'Sales2016']].groupby(['Region']).agg(['sum'])

ax = Sales_comparison.plot(kind='bar', stacked=True, legend=False,color=['navy','darkred'])
for i, label in enumerate(list(Sales_comparison.index)):
    S16 = int(Sales_comparison.loc[label]['Sales2016'][0])
    ax.annotate(str(S16),(i-0.2,S16+0.2*S16),color='white')
    S15 = int(Sales_comparison.loc[label]['Sales2015'][0])
    ax.annotate(str(S15),(i-0.2,S15-0.5*S15),color='white')

and result in the following image: End Result with Labels

Max
  • 1
  • 2