I have a Dataframe with a column (say 'Col') with values either from this list ['PO101','NI101','NE101'].
Count is:
- PO101 = 30000
- NI101 = 5000
- NE101 = 3000
I am trying to show how many are which on a stacked bar chart.
I created the stacked chart using following code:
df.assign(dummy=1).groupby(['dummy','Col']).size().to_frame().unstack().plot(
kind='bar',
stacked=True,
legend=True)
This creates the chart but the legend has weird tuple values with dummy included as below:
So I turned the legend as False in the above and used the following manner to generate new legend.
current_handles, _ = plt.gca().get_legend_handles_labels()
reversed_handles = reversed(current_handles)
labels = reversed(df['Col'].unique())
plt.legend(reversed_handles,labels,loc='lower right')
This generated the legend with proper names, however, it doesn't show me the correct color code as seen below:
Green (which is the largest portion in the chart) should have been PO101, instead it shows as NI101.
Can someone explain me why?
I think that the order which it follows to generate the chart and the order with which unique values are listed might be different.
Appreciating some guidance here.
EDIT: Attaching screenshots of the chart output for your reference.