简体   繁体   中英

Python & Matplotlib : Using Numpy.Array for Stacked Bar Graph

I'm trying to display a graph that includes my data using Stacked Bar Charts. My data is;

new_total = [[5,3,11,17,2,1,5,38,30,45,15,0],[8,21,13,54,21,7,20,103,114,149,77,15],[0,2,6,7,2,6,2,6,22,0,3,0],[0,9,3,11,10,0,0,26,47,17,7,9],[0,11,4,2,5,1,10,35,35,19,16,0],[0,0,0,2,0,0,2,5,6,16,4,3]]

It has 6 elements and every elements represents a color (and every element has 12 subelements). To explain with my codes and pictures;

width = 0.5
ind = np.arange(N)  # the x locations for the groups
temp = []
myMax = 0
myCount = 0
for x in range(len(movies)):
    myCount = myCount + 1
    if myCount == 1:
        self.axes.bar(ind, new_total[0], width)
        temp = np.zeros(N)
    else:
        if x == len(movies) - 1:
            myMax = max(np.add(temp, new_total[x - 1]))
        self.axes.bar(ind, new_total[x], width, bottom=np.add(temp, new_total[x - 1]))

If I'm using this code above; this graph is displayed. As you see for example; purple area is in blue area on somewhere. And total numbers (as you see on left) are wrong.

But if I use this code below;

self.axes.bar(ind, new_total[0], width)
self.axes.bar(ind, new_total[1], width, bottom=np.array(new_total[0]))
self.axes.bar(ind, new_total[2], width, bottom=np.add(new_total[0],new_total[1])) #I could use np.array(new_total[0]) + np.array(new_total[1])
self.axes.bar(ind, new_total[3], width, bottom=np.array(new_total[0]) + np.array(new_total[1]) + np.array(new_total[2]))
self.axes.bar(ind, new_total[4], width, bottom=np.array(new_total[0]) + np.array(new_total[1]) + np.array(new_total[2]) + np.array(new_total[3]))
self.axes.bar(ind, new_total[5], width, bottom=np.array(new_total[0]) + np.array(new_total[1]) + np.array(new_total[2]) + np.array(new_total[3]) + np.array(new_total[4]))

This graph is displayed and thats the graph which I want that shows the colors and total numbers perfectly. But I think this solution is so primitive. Because, sometimes the new_total will have 5 or 7 elements whatever. Can you fix my solution with a perfect way (it can has for loop or whatever)

I have not tested the code since you have classes and it's not quite minimal working snippet. However, as you can see from your last example, you increase the index by 1 (seems great for for loop ). Then you see that the bottom is always the sum of all previous elements, which again goes well with for loop, but here slice notation comes in handy. So given this, the code should look something like this:

for i, sublist in enumerate(new_total):
    self.axes.bar(ind, sublist, bottom=np.sum(new_total[0:i], axis=0), width=width)

A slight caveat is to use, np.sum() function with axis=0 which will sum your array element-wise).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM