Suppose I have dataframe called "market" which full database of my transaction. I grouped it by each month and product, so I get Sum of sales (USD) for each product/month
test=market.groupby(['Product','Month']).sum()['Sales'].unstack()
test
And the result from above like this
month/product 1 2 3 4 5 6 7 8 9 10 11 12
milk 12 13 12 13 21 21 9 10 15 17 16 20
bread 15 13 13 12 25 24 11 5 13 13 17 14
rice 12 13 15 13 21 21 9 10 19 11 16 25
coffee 12 13 12 16 25 23 11 20 15 14 11 15
tea 15 13 12 13 25 24 11 5 13 8 17 19
How I can make a graph with stacked product sales?
x-axis = month with stacked sales for each product
y-axis = Sales
What I already did:
First Attempt:
fig, sumbu = plt.subplots(figsize=(5,3))
graph=test.plot.bar(stacked='True', ax=sumbu)
plt.xlabel('Month', fontsize='12', color='red')
plt.ylabel('Sales', fontsize='12', color='red')
plt.xticks(rotation=45, size=8)
plt.show(graph)
It run but X-axis is product (I expected month)
Second attempt:
fig, sumbu = plt.subplots(figsize=(5,3))
graph=test.plot.bar(x='Month', y='Sales', stacked=True, ax=sumbu)
plt.xlabel('Month', fontsize='12', color='red')
plt.ylabel('Sales', fontsize='12', color='red')
plt.xticks(rotation=45, size=8)
plt.show(graph)
"KeyError: 'Month'