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

Stacked Bar ChartsStacked Bar Charts

Stacked bar charts are useful when we want to compare several categories (two or more) for each value on the x-axis. For example, instead of only looking at the GDP of different countries we may want to look at the amount of contribution of each economic sector to the GDP of a particular country (the data is not real):

Code Description
plt.bar(countries, primary_sector)

Plotting the lower bars for primary_sector (blue bars).

plt.bar(countries, secondary_sector, bottom=primary_sector)

Plotting the middle bars for secondary_sector (orange bars) on top of the lower bars for primary_sector.

plt.bar(countries, tertiary_sector, bottom=primary_sector + secondary_sector)

Plotting the upper bars for tertiary_sector (green bars) on top of the middle bars.

Similarly to line plots and scatter plots, we called the bar() function three times to create three bars for each value on the x-axis (country names in our example). In every call countries are specified as x-axis values in order to create stacked bars. Pay extra attention to the bottom parameter.

Note

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

Завдання

  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.

Все було зрозуміло?

Секція 2. Розділ 5
toggle bottom row
course content

Зміст курсу

Ultimate Visualization with Python

Stacked Bar ChartsStacked Bar Charts

Stacked bar charts are useful when we want to compare several categories (two or more) for each value on the x-axis. For example, instead of only looking at the GDP of different countries we may want to look at the amount of contribution of each economic sector to the GDP of a particular country (the data is not real):

Code Description
plt.bar(countries, primary_sector)

Plotting the lower bars for primary_sector (blue bars).

plt.bar(countries, secondary_sector, bottom=primary_sector)

Plotting the middle bars for secondary_sector (orange bars) on top of the lower bars for primary_sector.

plt.bar(countries, tertiary_sector, bottom=primary_sector + secondary_sector)

Plotting the upper bars for tertiary_sector (green bars) on top of the middle bars.

Similarly to line plots and scatter plots, we called the bar() function three times to create three bars for each value on the x-axis (country names in our example). In every call countries are specified as x-axis values in order to create stacked bars. Pay extra attention to the bottom parameter.

Note

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

Завдання

  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.

Все було зрозуміло?

Секція 2. Розділ 5
toggle bottom row
some-alt