I need some help making a set of stacked bar charts in python with matlibplot. My basic code is below but my problems is how to generate the value for bottom fo
I solved it like this:
import numpy as np
dates = # somehow get a list of dates
labels = # a list of various labels
colors = # somehow get a list of colors
margin_bottom = np.zeros(dates)
for index, label in enumerate(labels):
values = # get your values for the label at index-th position from somewhere
ax.bar(
dates, values,
align='center', label=label, color=colors[index], bottom=margin_bottom
)
margin_bottom += values # here you simply add it to the previous margin
# margin_bottom is a numpy array, adding a list will not change that
It's similar to some other solutions, but it doesn't require all of the margins being stored at all time. Instead it "builds" the stacks from bottom up, adding more and more margin with each iteration.