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:

17 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:

10 Likes

I think zip is useful here.

bottom_3 = [v1 + v2 for (v1, v2) in zip(sales1, sales2)]
6 Likes

shouldn’t you add a plt.legend() at the end?

Why is it OK to stack Sales 2 on top of Sales 1 when Sales 2 has values that are smaller than Sales 1 (36 < 56, for instance). Doesn’t this become misleading?

I don’t think so. The goal of using stack charts in this case is to show the total between the sales of the two locations while preserving knowledge of each of them, so the viewer can easily identify how good the business on each location and how many sales were made in total.

1 Like