I am trying to make a multiple stacked bar plot with pandas.
Here is a sample code:
import pandas as pd
df = pd.DataFrame({'a':[10, 20], 'b': [15, 25], 'c': [35, 40], 'd':[45, 50]}, index=['john', 'bob'])
ax = df[['a', 'c']].plot.bar(position=0, width=0.1, stacked=True)
df[['b', 'd']].plot.bar(position=1, width=0.1, stacked=True, ax=ax)
Which, in a notebook, give the following output: 
The problem I'm facing is that, for each group, the bars are not in the order I want them to be. The doc says that the "position" argument for a bar plot specifies the relative position of the bar, with 0 being left and 1 being right. But it seems to do the complete opposite. What am I misunderstanding?
The position parameter in pd.Df.plotcontrols the alignment of your bar plot layouts. It resembles similarity with the align parameter of a matplotlib bar plot.
Illustrations:
1. when position=0.5: [Default : center alignment]
df[['a', 'c']].plot.bar(position=0.5, width=0.1, stacked=True, ax=ax)
df[['b', 'd']].plot.bar(position=0.5, width=0.1, stacked=True, ax=ax)
The bars are coinciding with the X-axis labels as shown:
2. when position=0:[left/bottom edge alignment]
df[['a', 'c']].plot.bar(position=0, width=0.1, stacked=True, ax=ax)
df[['b', 'd']].plot.bar(position=0, width=0.1, stacked=True, ax=ax)
The left most portion of the bars are coinciding with the X-axis labels as shown:

Now, you can clearly distinguish the difference between the above 2 figures.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With