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

Зміст курсу

Ultimate Visualization with Python

Grouped Bar ChartsGrouped 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:

Code Description
for i in range(len(sectors)):


Iterating over each sector array (primary_sector, secondary_sector, tertiary_sector) within the sectors 2D array.


plt.bar(positions + width * i, sectors[i], width)

    positions + width * i determines the x-positions of the bars for the current sector;
    positions set the x-coordinates for each country;
    width * i shifts the bars horizontally to separate them for each sector;
    sectors[i] provides the data for the heights of the bars, with i specifying the current sector's data;
    width sets the bar width, so that each bar in a group is right next to the other one without overlapping.

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.

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

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

Зміст курсу

Ultimate Visualization with Python

Grouped Bar ChartsGrouped 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:

Code Description
for i in range(len(sectors)):


Iterating over each sector array (primary_sector, secondary_sector, tertiary_sector) within the sectors 2D array.


plt.bar(positions + width * i, sectors[i], width)

    positions + width * i determines the x-positions of the bars for the current sector;
    positions set the x-coordinates for each country;
    width * i shifts the bars horizontally to separate them for each sector;
    sectors[i] provides the data for the heights of the bars, with i specifying the current sector's data;
    width sets the bar width, so that each bar in a group is right next to the other one without overlapping.

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.

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

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