Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Grouped 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

bookGrouped Bar Charts

In the previous chapter we used a stacked bar chart to compare several categories for each value on the x-axis. However, there is another option to accomplish this task by using a grouped bar chart. It allows us to visualize such data via placing bars on the sides of other bars instead of placing them on top of each other.

Here are the steps to achieve this:

  1. Start by specifying the bar width and creating an array of x-axis coordinates (you can use np.arange() for this);
  2. Create a two-dimensional array to hold the arrays for each category;
  3. Use a for loop to call the bar() function multiple times, once for each category (e.g., three times for three categories).

Let's have a look at an example:

12345678910111213141516
import matplotlib.pyplot as plt import numpy as np countries = ['USA', 'China', 'Japan'] positions = np.arange(len(countries)) 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]) sectors = np.array([primary_sector, secondary_sector, tertiary_sector]) # Setting the width of the bars width = 0.3 for i in range(len(sectors)): # Plotting the bars for each category (sector) plt.bar(positions + width * i, sectors[i], width) # Setting the x-axis ticks position and labels plt.xticks(positions + width * (len(sectors) - 1) / 2, countries) plt.show()
copy

The xticks() function customizes the x-axis in the following way:

  • The first argument positions + width * (len(sectors) - 1) / 2 is an array of x-axis coordinates representing the centers of the bar groups;
  • The second argument provides labels (names) for these x-axis ticks, using the countries array.

Note

The code in our example is rather flexible and works for an arbitrary number of categories (you may only need to adjust width to avoid overlapping).

Tarea

  1. Pass the correct array to the len() function.
  2. Use the correct function to plot bars.
  3. Use the correct variable which should be multiplied by i.
  4. Use the correct variable as an index for answers array.
  5. Pass the correct variable as the rightmost arguments of the plotting function.

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 6
toggle bottom row

bookGrouped Bar Charts

In the previous chapter we used a stacked bar chart to compare several categories for each value on the x-axis. However, there is another option to accomplish this task by using a grouped bar chart. It allows us to visualize such data via placing bars on the sides of other bars instead of placing them on top of each other.

Here are the steps to achieve this:

  1. Start by specifying the bar width and creating an array of x-axis coordinates (you can use np.arange() for this);
  2. Create a two-dimensional array to hold the arrays for each category;
  3. Use a for loop to call the bar() function multiple times, once for each category (e.g., three times for three categories).

Let's have a look at an example:

12345678910111213141516
import matplotlib.pyplot as plt import numpy as np countries = ['USA', 'China', 'Japan'] positions = np.arange(len(countries)) 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]) sectors = np.array([primary_sector, secondary_sector, tertiary_sector]) # Setting the width of the bars width = 0.3 for i in range(len(sectors)): # Plotting the bars for each category (sector) plt.bar(positions + width * i, sectors[i], width) # Setting the x-axis ticks position and labels plt.xticks(positions + width * (len(sectors) - 1) / 2, countries) plt.show()
copy

The xticks() function customizes the x-axis in the following way:

  • The first argument positions + width * (len(sectors) - 1) / 2 is an array of x-axis coordinates representing the centers of the bar groups;
  • The second argument provides labels (names) for these x-axis ticks, using the countries array.

Note

The code in our example is rather flexible and works for an arbitrary number of categories (you may only need to adjust width to avoid overlapping).

Tarea

  1. Pass the correct array to the len() function.
  2. Use the correct function to plot bars.
  3. Use the correct variable which should be multiplied by i.
  4. Use the correct variable as an index for answers array.
  5. Pass the correct variable as the rightmost arguments of the plotting function.

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 6
toggle bottom row

bookGrouped Bar Charts

In the previous chapter we used a stacked bar chart to compare several categories for each value on the x-axis. However, there is another option to accomplish this task by using a grouped bar chart. It allows us to visualize such data via placing bars on the sides of other bars instead of placing them on top of each other.

Here are the steps to achieve this:

  1. Start by specifying the bar width and creating an array of x-axis coordinates (you can use np.arange() for this);
  2. Create a two-dimensional array to hold the arrays for each category;
  3. Use a for loop to call the bar() function multiple times, once for each category (e.g., three times for three categories).

Let's have a look at an example:

12345678910111213141516
import matplotlib.pyplot as plt import numpy as np countries = ['USA', 'China', 'Japan'] positions = np.arange(len(countries)) 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]) sectors = np.array([primary_sector, secondary_sector, tertiary_sector]) # Setting the width of the bars width = 0.3 for i in range(len(sectors)): # Plotting the bars for each category (sector) plt.bar(positions + width * i, sectors[i], width) # Setting the x-axis ticks position and labels plt.xticks(positions + width * (len(sectors) - 1) / 2, countries) plt.show()
copy

The xticks() function customizes the x-axis in the following way:

  • The first argument positions + width * (len(sectors) - 1) / 2 is an array of x-axis coordinates representing the centers of the bar groups;
  • The second argument provides labels (names) for these x-axis ticks, using the countries array.

Note

The code in our example is rather flexible and works for an arbitrary number of categories (you may only need to adjust width to avoid overlapping).

Tarea

  1. Pass the correct array to the len() function.
  2. Use the correct function to plot bars.
  3. Use the correct variable which should be multiplied by i.
  4. Use the correct variable as an index for answers array.
  5. Pass the correct variable as the rightmost arguments of the plotting function.

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!

In the previous chapter we used a stacked bar chart to compare several categories for each value on the x-axis. However, there is another option to accomplish this task by using a grouped bar chart. It allows us to visualize such data via placing bars on the sides of other bars instead of placing them on top of each other.

Here are the steps to achieve this:

  1. Start by specifying the bar width and creating an array of x-axis coordinates (you can use np.arange() for this);
  2. Create a two-dimensional array to hold the arrays for each category;
  3. Use a for loop to call the bar() function multiple times, once for each category (e.g., three times for three categories).

Let's have a look at an example:

12345678910111213141516
import matplotlib.pyplot as plt import numpy as np countries = ['USA', 'China', 'Japan'] positions = np.arange(len(countries)) 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]) sectors = np.array([primary_sector, secondary_sector, tertiary_sector]) # Setting the width of the bars width = 0.3 for i in range(len(sectors)): # Plotting the bars for each category (sector) plt.bar(positions + width * i, sectors[i], width) # Setting the x-axis ticks position and labels plt.xticks(positions + width * (len(sectors) - 1) / 2, countries) plt.show()
copy

The xticks() function customizes the x-axis in the following way:

  • The first argument positions + width * (len(sectors) - 1) / 2 is an array of x-axis coordinates representing the centers of the bar groups;
  • The second argument provides labels (names) for these x-axis ticks, using the countries array.

Note

The code in our example is rather flexible and works for an arbitrary number of categories (you may only need to adjust width to avoid overlapping).

Tarea

  1. Pass the correct array to the len() function.
  2. Use the correct function to plot bars.
  3. Use the correct variable which should be multiplied by i.
  4. Use the correct variable as an index for answers array.
  5. Pass the correct variable as the rightmost arguments of the plotting function.

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 6
Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
some-alt