I am plotting a stacked bar chart for a few fixed quantities using the usual:
bar = df.plot.barh(x='Intervention', y={'Remuneration','Training','Supervision'}, stacked=True )
I however noticed that depending on the input dataset, matplotlib changes the order of the plotted columns. For instance, sometimes it plots Remuneration as the first component of the stacked bar chart, whereas in other occasions it changes it to Training or Supervision. To be honest, I haven't been able to figure out what is the order being used. Ideally I'd like to keep always the same order as I have a list of colors to be used. Is there any way to force this re-ordering? Eg that the stacked bar chart always appears as Remuneration-Training-Supervision?
CodePudding user response:
to set a custom order for the stacked bars, you will need to order it by setting the CategoricalIndex
and sorting the data by these categories. This will sort the three categories in the way you need. A small example to show the same is below...
Data 'df'
Intervention Remuneration Training Supervision
A 21 4 12
B 41 5 21
C 33 6 7
Code
#Convert Intervention as index, so columns are the categories
df = df.set_index('Intervention')
#Set categories... Order will Remuneration-Training-Supervision
df.columns=pd.CategoricalIndex(df.columns.values, ordered=True, categories=['Remuneration','Training','Supervision'])
#Sort the data
df = df.sort_index(axis=1)
#...and plot
bar = df.plot.barh(stacked=True)
Output