Contenido del Curso
Ultimate Visualization with Python
Ultimate Visualization with Python
Grouped 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:
-
Start by specifying the bar width and creating an array of x-axis coordinates (you can use
np.arange()
for this); -
Create a two-dimensional array to hold the arrays for each category;
-
Use a
for
loop to call thebar()
function multiple times, once for each category (e.g., three times for three categories).
Let's have a look at an example:
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()
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).
Swipe to show code editor
- Pass the correct array to the
len()
function. - Use the correct function to plot bars.
- Use the correct variable which should be multiplied by
i
. - Use the correct variable as an index for
answers
array. - Pass the correct variable as the rightmost arguments of the plotting function.
¡Gracias por tus comentarios!
Grouped 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:
-
Start by specifying the bar width and creating an array of x-axis coordinates (you can use
np.arange()
for this); -
Create a two-dimensional array to hold the arrays for each category;
-
Use a
for
loop to call thebar()
function multiple times, once for each category (e.g., three times for three categories).
Let's have a look at an example:
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()
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).
Swipe to show code editor
- Pass the correct array to the
len()
function. - Use the correct function to plot bars.
- Use the correct variable which should be multiplied by
i
. - Use the correct variable as an index for
answers
array. - Pass the correct variable as the rightmost arguments of the plotting function.
¡Gracias por tus comentarios!