Can we stack more than 2 bars in a bar chart?

Question

In the context of this exercise, can we stack more than 2 bars on top of each other?

Answer

Yes, you can absolutely stack more than 2 bars on a bar chart.

In order to do this, the bottom of each subsequent set of bars should equal the total heights of the bars below them.

Example

# Given the follow y values for 3 groups of data
group1 = [1, 1, 1, 1]
group2 = [2, 2, 2, 2]
group3 = [3, 3, 3, 3]

x = [0, 1, 2, 3] # x positions

# First layer of bars
plt.bar(x, group1)

# Second layer of bars
plt.bar(x, group2, bottom=group1)

# Adding a third layer of bars.
# Calculate the bottom height of the third layer
# by adding together all lower layer heights.
bottom_of_3 = [1+2, 1+2, 1+2, 1+2] # [3, 3, 3, 3]

# Third layer of bars
plt.bar(x, group3, bottom=bottom_of_3)
4 Likes

You can also add up those numbers using numpy arrays - helpful when dealing with large lists of data:

import numpy as np # importing numpy
from matplotlib import pyplot as plt

sales1 =  [91, 76, 56, 66, 52, 27]
sales2 = [65, 82, 36, 68, 38, 40]
sales3 = [32, 35, 55, 95, 23, 2] # I just made these numbers up

plt.bar(range(len(sales1)), sales1, label="Location 1")
plt.bar(range(len(sales2)), sales2, bottom=sales1, label="Location 2")

# Creating a variable that contains an array of values 
# equal to sales1 + sales2 at each index
bottom_of_3 = np.array(sales1) + np.array(sales2)

# Creating third layer of bars using the new variable
plt.bar(range(len(sales3)), sales3, bottom=bottom_of_3, label="Location 3")

*Note that the use of numpy for stacked bar charts comes up in a later lesson. Thanks Codecademy :slight_smile:

7 Likes

We can even use list comprehensions for the same.

sales1 =  [91, 76, 56, 66, 52, 27]
sales2 = [65, 82, 36, 68, 38, 40]
sales = [25,32, 37, 35, 55, 23] # I just made these numbers up

plt.bar(range(len(sales1)), sales1, label="Location 1")
plt.bar(range(len(sales2)), sales2, bottom=sales1, label="Location 2")

# Creating a variable that contains an array of values equal to sales1 + sales2 at each index

bottom_3 = [sales1[i] + sales2[i] for i in range(len(sales1))]
plt.bar(range(len(sales)), sales, bottom = bottom_3, label = "Location 3")
plt.show()

Hope this helps :slight_smile:

2 Likes

I think zip is useful here.

bottom_3 = [v1 + v2 for (v1, v2) in zip(sales1, sales2)]
1 Like