Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Stacked Bar Charts | Creating Commonly Used Plots
Ultimate Visualization with Python

Swipe to show menu

book
Stacked Bar Charts

Stacked bar charts allow comparison of multiple categories within each x-axis group. For example, rather than showing only the total GDP of each country, they can illustrate the contribution of individual economic sectors to the total.

1234567891011
import matplotlib.pyplot as plt import numpy as np countries = ['USA', 'China', 'Japan'] primary_sector = np.array([1.4, 4.8, 0.4]) secondary_sector = np.array([11.3, 6.2, 0.8]) tertiary_sector = np.array([14.2, 8.4, 3.2]) # Calling the bar() function multiple times for each category (sector) plt.bar(countries, primary_sector) plt.bar(countries, secondary_sector, bottom=primary_sector) plt.bar(countries, tertiary_sector, bottom=primary_sector + secondary_sector) plt.show()
copy

To create stacked bars, the bar() function is called multiple timesβ€”once for each sector. In each call, the same countries list is used for the x-axis, and the bottom parameter ensures that each new segment is stacked on top of the previous one.

Note
Study More

The bottom parameter specifies the y coordinate(s) of the bottom side(s) of the bars. Here is the bar() documentation.

Task

Swipe to start coding

  1. Use the correct function for creating bar charts.
  2. Plot the lower bars for yes_answers.
  3. Plot the bars for no_answers on top of the bars for yes_answers with specifying the correct keyword argument.

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 5
We're sorry to hear that something went wrong. What happened?

Ask AI

expand
ChatGPT

Ask anything or try one of the suggested questions to begin our chat

book
Stacked Bar Charts

Stacked bar charts allow comparison of multiple categories within each x-axis group. For example, rather than showing only the total GDP of each country, they can illustrate the contribution of individual economic sectors to the total.

1234567891011
import matplotlib.pyplot as plt import numpy as np countries = ['USA', 'China', 'Japan'] primary_sector = np.array([1.4, 4.8, 0.4]) secondary_sector = np.array([11.3, 6.2, 0.8]) tertiary_sector = np.array([14.2, 8.4, 3.2]) # Calling the bar() function multiple times for each category (sector) plt.bar(countries, primary_sector) plt.bar(countries, secondary_sector, bottom=primary_sector) plt.bar(countries, tertiary_sector, bottom=primary_sector + secondary_sector) plt.show()
copy

To create stacked bars, the bar() function is called multiple timesβ€”once for each sector. In each call, the same countries list is used for the x-axis, and the bottom parameter ensures that each new segment is stacked on top of the previous one.

Note
Study More

The bottom parameter specifies the y coordinate(s) of the bottom side(s) of the bars. Here is the bar() documentation.

Task

Swipe to start coding

  1. Use the correct function for creating bar charts.
  2. Plot the lower bars for yes_answers.
  3. Plot the bars for no_answers on top of the bars for yes_answers with specifying the correct keyword argument.

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 5
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
We're sorry to hear that something went wrong. What happened?
some-alt