Side by side bar plot

In the lesson of Introduction to Matplotlib, in side-by-side bars lesson, I tried to label the x-axis but it did not work - see my code below. It shows the pot alright but I cannot make it to show the labels. The last three lines of codes do not make any changes in the plot!

The lesson, unfortunately, does not show how to create a label for the x-axis in the side-by-side bar plot. Then, I tried to use the same code on my computer. Now I get a strange error. It says plt.show() is a syntax error!!

I appreciate any help.

My code:

drinks = [“cappuccino”, “latte”, “chai”, “americano”, “mocha”, “espresso”]
sales1 = [91, 76, 56, 66, 52, 27]
sales2 = [65, 82, 36, 68, 38, 40]

#Paste the x_values code here

n = 1 # This is our first dataset (out of 2)
t = 2 # Number of datasets
d = 6 # Number of sets of bars
w = 0.8 # Width of each bar
store1_x = [telement + wn for element
in range(d)]

plt.bar(store1_x, sales1)

n = 2 # This is our first dataset (out of 2)
t = 2 # Number of datasets
d = 6 # Number of sets of bars
w = 0.8 # Width of each bar
store2_x = [telement + wn for element
in range(d)]

plt.bar(store2_x, sales2)

ax = plt.subplot()

ax.set_xticklabels(drinks)
ax.set_xticks(range(len(drinks))
plt.show()

Hi! Welcome to the forums :slight_smile:

Look at this line, the issue is not in plt.show(), but the line before…

ax.set_xticks(range(len(drinks))   # what is missing here?
plt.show()

For future reference, when posting code you have to first press the </> and then paste the code inside (to keep the formatting). This is crucial for syntax errors.

Thank you very much.

I fixed my error. Now; it shows the plot, but the labels on x-axis are not showing correctly. How can I adjust xticks labels to show up right in the middle of each set of bars?

import codecademylib
from matplotlib import pyplot as plt

drinks = ["cappuccino", "latte", "chai", "americano", "mocha", "espresso"]
sales1 =  [91, 76, 56, 66, 52, 27]
sales2 = [65, 82, 36, 68, 38, 40]

#Paste the x_values code here

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

plt.bar(store1_x, sales1)

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

plt.bar(store2_x, sales2)

ax = plt.subplot()

ax.set_xticklabels(drinks)
ax.set_xticks(range(len(drinks)))
plt.show()

Try checking documentation instructions for this and see if it helps. I would experiment with different inputs and see how it affects it.

https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.xticks.html

I figured it out. Here is my code:

drinks = ["cappuccino", "latte", "chai", "americano", "mocha", "espresso"]
sales1 =  [91, 76, 56, 66, 52, 27]
sales2 = [65, 82, 36, 68, 38, 40]

#Paste the x_values code here

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

print(store1_x)             

plt.bar(store1_x, sales1)

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

xt = [ (i+j)/2 for (i,j) in zip(store1_x,store2_x)]

plt.bar(store2_x, sales2)

ax = plt.subplot()


ax.set_xticks(xt)
ax.set_xticklabels(drinks)
plt.title('Number of Drink Sold at Each Locaiton')
plt.legend(['Location 1','Location 2'])
plt.show()