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 for any element beyond the 2nd one efficiently. I can get the example graph to stack correctly (always a,b,c,d from bottom to top)
import numpy as np
import matplotlib.pyplot as plt
ind = np.arange(3)
a = [3,6,9]
b = [2,7,1]
c = [0,3,1]
d = [4,0,3]
p1 = plt.bar(ind, a, 1, color='#ff3333')
p2 = plt.bar(ind, b, 1, color='#33ff33', bottom=a)
p3 = plt.bar(ind, c, 1, color='#3333ff', bottom=[a[j] +b[j] for j in range(len(a))])
p4 = plt.bar(ind, d, 1, color='#33ffff', bottom=[a[j] +b[j] +c[j] for j in range(len(a))])
plt.show()
My final code could have very large number of bars and the ever expanding function bottom = [...] cannot be the best solution. It would be great if you could also explain how I need to derive the value. Is there a numpy function.
Thank you very much!!! PS I have searched for an answer but I did not understand what I could find.