1

I have a pandas series with a multiindex like this:

my_series.head(5)
datetime_publication  my_category
2015-03-31            xxx                  24
                      yyy                  2
                      zzz                  1
                      qqq                  1
                      aaa                  2
dtype: int64

I am generating a horizontal bar chart using the plot method from pandas with all those stacked categorical values divided by datetime (according to the index hierarchy) like this:

my_series.unstack(level=1).plot.barh(
    stacked=True, 
    figsize=(16,6),
    colormap='Paired', 
    xlim=(0,10300), 
    rot=45
)
plt.legend(
    bbox_to_anchor=(0., 1.02, 1., .102), 
    loc=3, 
    ncol=5, 
    mode="expand", 
    borderaxespad=0.
)

However I am not able to find a way to normalize all those values in the series broken down by datetime_publication,my_category. I would like to have all the horizontal bars of the same length, but right now the legth depends on the absolute values in the series.

horizontal bar chart - stacked

Is there a built-in functionality from pandas to normalize the slices of the series or some quick function to apply at the series that keeps track of the total taken from the multiindex combinatin of the levels?

TPPZ
  • 4,447
  • 10
  • 61
  • 106
  • You probably want to normalize the data before plotting. Have you tried any of the techniques mentioned [here](https://stackoverflow.com/questions/26414913/normalize-columns-of-pandas-data-frame)? – Nathan Watkins Jun 06 '17 at 16:25
  • I've done something like this `from sklearn.preprocessing import MinMaxScaler scaler = MinMaxScaler() my_series.groupby(['datetime_publication','my_category']).apply(lambda x: scaler.fit_transform(x))` but that `scikit-learn` function gets applied to each scalar of the slices rather than the slice itself – TPPZ Jun 06 '17 at 21:44
  • It looks like you need to create a "100% stacked bar chart" by converting the normalized categories to a percentage of their respective day's total. Does this [question/answer](https://stackoverflow.com/questions/36784336/python-pandas-plotting-100-stacked-graph-issue) address what you're looking for? – Nathan Watkins Jun 07 '17 at 13:12

0 Answers0