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
course content

Contenido del Curso

Ultimate Visualization with Python

Ultimate Visualization with Python

1. Matplotlib Introduction
2. Creating Commonly Used Plots
3. Plots Customization
4. More Statistical Plots
5. Plotting with Seaborn

bookStacked 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):

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

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.

Tarea

  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.

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 2. Capítulo 5
toggle bottom row

bookStacked 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):

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

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.

Tarea

  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.

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 2. Capítulo 5
toggle bottom row

bookStacked 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):

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

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.

Tarea

  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.

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

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

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

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.

Tarea

  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.

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
Sección 2. Capítulo 5
Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
some-alt