Creating stacked bar charts using Matplotlib can be difficult. Often the data you need to stack is oriented in columns, while the default Pandas bar plotting function requires the data to be oriented in rows with a unique column for each layer.
Below is an example dataframe, with the data oriented in columns. In this case, we want to create a stacked plot using the
Year column as the x-axis tick mark, the
Month column as the layers, and the
Value column as the height of each month band.
I have seen a few solutions that take a more iterative approach, creating a new layer in the stack for each category. This is accomplished by using the same axis object
ax to append each band, and keeping track of the next bar location by cumulatively summing up the previous heights with a
Using a Pivot
The above approach works pretty well, but there has to be a better way. After a little bit of digging, I found a better solution using the Pandas pivot function.
The pivot function takes arguments of
index (what you want on the x-axis),
columns (what you want as the layers in the stack), and
values (the value to use as the height of each layer). Note that there needs to be a unique combination of your
column values for each number in the
values column in order for this to work.
The end result is a new dataframe with the data oriented so the default Pandas stacked plot works perfectly.