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

Зміст курсу

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

Завдання

  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 desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 2. Розділ 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).

Завдання

  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 desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 2. Розділ 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).

Завдання

  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 desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

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

Завдання

  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 desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Секція 2. Розділ 6
Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
some-alt