0

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)

2
  • Why don't use for loop if that's the case? Commented Apr 10, 2019 at 14:27
  • thats why I'm asking that how can I use for loop for add (... + np.array(new_total[i] ) Commented Apr 10, 2019 at 14:29

1 Answer 1

1

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).

Sign up to request clarification or add additional context in comments.

3 Comments

Let me know how it goes, in case you need any more help.
so much thanks man, it worked. I have 1 more challenges, if you can help I will be very happy. How can I order the dates in chronological order with their bars?
you'll need to put in a bit more info as I can't see from where you get the dates.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.