Search code examples
pythonpandasmatplotlibhistogramstacked-chart

Stacked-bar in sub-plot using df with more than two columns


My df contains three columns of monthly data. Here is the file.
I want to plot all three columns for each month on top each other in a subplot using a stacked-bar. This is one of many codes I have tried:

df.plot(kind='bar', stacked=True, bottom = df['Yf'])

It creates a single plot with bars hang strangely (see Figure below).
enter image description here

I want to put it in a subplot and all columns are stacked together. So, the highest points in each month are the sum of the three parameters in corresponding months. I also want to have the freedom to arrange which parameter should go to the bottom, middle, and top.
I want something like this.
enter image description here Searched on the internet, no solution yet.


Solution

  • This workaround gives what I need.

    ax1.bar(df.index, df['Yf'],  alpha=0.7, color='green')
    ax1.bar(df.index, df['Lc'],  bottom=df['Yf'], alpha=0.7, color='red')
    ax1.bar(df.index, df['Ls'],  bottom=df['Yf']+df['Lc'], alpha=0.7, color='c')
    

    enter image description here

    Thought there was a single line ax1.bar solution.