Learn

We can use a bar chart to compare two sets of data with the same types of axis values. To do this, we plot two sets of bars next to each other, so that the values of each category can be compared. For example, here is a chart with side-by-side bars for the populations of the United States and China over the age of 65 (in percentages): (data taken from World Bank)

Some examples of data that side-by-side bars could be useful for include:

• the populations of two countries over time
• prices for different foods at two different restaurants
• enrollments in different classes for males and females

In the graph above, there are 7 sets of bars, with 2 bars in each set. Each bar has a width of 0.8 (the default width for all bars in Matplotlib).

• If our first blue bar is at x=0, then we want the next blue bar to be at x=2, and the next to be at x=4, etc.
• Our first orange bar should be at x=0.8 (so that it is touching the blue bar), and the next orange bar should be at x=2.8, etc.

This is a lot of math, but we can make Python do it for us by copying and pasting this code:

``````# China Data (blue bars)
n = 1  # This is our first dataset (out of 2)
t = 2 # Number of datasets
d = 7 # Number of sets of bars
w = 0.8 # Width of each bar
x_values1 = [t*element + w*n for element
in range(d)]``````

That just generated the first set of x-values. To generate the second set, paste the code again, but change `n` to 2, because this is the second dataset:

``````# US Data (orange bars)
n = 2  # This is our second dataset (out of 2)
t = 2 # Number of datasets
d = 7 # Number of sets of bars
w = 0.8 # Width of each bar
x_values2 = [t*element + w*n for element
in range(d)]``````

Let’s examine our special code:

``[t*element + w*n for element in range(d)]``

This is called a list comprehension. It’s a special way of generating a list from a formula. You can learn more about it in this article. For making side-by-side bar graphs, you’ll never need to change this line; just paste it into your code and make sure to define `n`, `t`, `d`, and `w` correctly.

In the instructions below, we’ll experiment with side-by-side bars to compare different locations of the MatplotSip coffee empire.

### Instructions

1.

The second location of MatplotSip recently opened up, and the owners want to compare the drink choices of the clientele at the two different locations. To do this, it will be helpful to have the sales of each drink plotted on the same axes. We have provided `sales2`, a list of values representing the sales of the same drinks at the second MatplotSip location.

2.

Paste the following code into script.py:

``````n = ?  # This is our first dataset (out of 2)
t = ? # Number of datasets
d = ? # Number of sets of bars
w = ? # Width of each bar
x_values = [t*element + w*n for element
in range(d)]``````

Update the `?` with the correct values to generate your first set of x-values.

Update `x_values` to be `store1_x` (this makes it more clear which set of x-values we are making).

3.

Use the `plt.bar` to position the bars corresponding to `sales1` on the plot. The x-values for `plt.bar` should be the `store1_x` list that you just created.

4.

Once more, paste the following code into script.py:

``````n = ?  # This is our second dataset (out of 2)
t = ? # Number of datasets
d = ? # Number of sets of bars
w = ? # Width of each bar
x_values = [t*element + w*n for element
in range(d)]``````

Update the `?` with the correct values to generate your second set of x-values.

Update `x_values` to be `store2_x` (this makes it more clear which set of x-values we are making).

5.

Call `plt.bar` again, with `sales2` as the y-values and your `store2_x` list as the x-values.

6.

Show the plot using `plt.show()`.