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

Swipe to show menu

book
Grouped Bar Charts

Another common option is a grouped bar chart, where bars for each category are placed side by side instead of stacking them.

This is useful when you want to compare categories across groups (like economic sectors in different countries), rather than within one total.

Steps to Create a Grouped Bar Chart

  1. Set a bar width and create an array for x-axis positions using np.arange();

  2. Combine your category data into a 2D array;

  3. Use a for loop to draw each group of bars with the bar() function, shifting their positions horizontally;

  4. Customize the x-axis tick positions and labels using plt.xticks().

12345678910111213141516171819202122232425
import matplotlib.pyplot as plt import numpy as np # Labels and data countries = ['USA', 'China', 'Japan'] positions = np.arange(len(countries)) primary = np.array([1.4, 4.8, 0.4]) secondary = np.array([11.3, 6.2, 0.8]) tertiary = np.array([14.2, 8.4, 3.2]) # Group the data sectors = np.array([primary, secondary, tertiary]) # Width of each bar width = 0.25 # Plot each group of bars for i in range(len(sectors)): plt.bar(positions + width * i, sectors[i], width) # Center the group of bars and label the ticks plt.xticks(positions + width, countries) plt.show()
copy

How xticks() Works

  • The first argument shifts the tick marks to the center of each group of bars;

  • The second argument sets the labels using the countries list.

Note
Note

This approach works for any number of categories β€” just adjust the width to make sure the bars don't overlap.

Task

Swipe to start coding

  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.

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 6

Ask AI

expand
ChatGPT

Ask anything or try one of the suggested questions to begin our chat

book
Grouped Bar Charts

Another common option is a grouped bar chart, where bars for each category are placed side by side instead of stacking them.

This is useful when you want to compare categories across groups (like economic sectors in different countries), rather than within one total.

Steps to Create a Grouped Bar Chart

  1. Set a bar width and create an array for x-axis positions using np.arange();

  2. Combine your category data into a 2D array;

  3. Use a for loop to draw each group of bars with the bar() function, shifting their positions horizontally;

  4. Customize the x-axis tick positions and labels using plt.xticks().

12345678910111213141516171819202122232425
import matplotlib.pyplot as plt import numpy as np # Labels and data countries = ['USA', 'China', 'Japan'] positions = np.arange(len(countries)) primary = np.array([1.4, 4.8, 0.4]) secondary = np.array([11.3, 6.2, 0.8]) tertiary = np.array([14.2, 8.4, 3.2]) # Group the data sectors = np.array([primary, secondary, tertiary]) # Width of each bar width = 0.25 # Plot each group of bars for i in range(len(sectors)): plt.bar(positions + width * i, sectors[i], width) # Center the group of bars and label the ticks plt.xticks(positions + width, countries) plt.show()
copy

How xticks() Works

  • The first argument shifts the tick marks to the center of each group of bars;

  • The second argument sets the labels using the countries list.

Note
Note

This approach works for any number of categories β€” just adjust the width to make sure the bars don't overlap.

Task

Swipe to start coding

  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.

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 6
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
We're sorry to hear that something went wrong. What happened?
some-alt