 # How to improve the x-axis labels with side by side bars?

Hello, everyone. This is the lesson i´ve been digging into.

Everything went smoothly for what was requested in the lesson, i got problems when i tried to improve by adjusting the x-axis with labels and correct it’s position (set the ticks in the middle of the bars) but everything i did didn’t change the result.

This is my code

``````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 second 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)

plt.show()
``````

I tried using the `ax = plt.subplot()` to change but nothing happened.

``````ax = plt.subplot()
ax.set_xticks(range(len(drinks)))
ax.set_xticklabels(drinks)
``````

I tried putting after both `store1_x` and `store2_x` blocks of code, but nothing happened with the results, i didn’t get an error, but the result didn’t change either. What is going on here? Is this something about my ax code or is the ticks and labels changed in other way for side by side bars?

Another quick question if i may, when defining the d (this doesn’t sound very good, but i don’t know how else to say this) in the `store_x` blocks, i tried using `range(len(drinks))` to get the number but the lesson didn’t accept as a correct answer. Why? Can’t i use this method to save me time in this case?

I might need to acutally go through the lesson but before then…
If you’ve already produced your figure, axis and plot and you want the axis handle you’d have to use something like `ax = plt.gca()` or start looking for the children of Figure instances or similar.

Something like ax = plt.subplot() would create a new axis instance and would not allow you to change the plot you have for a different instance.
Once you have the right reference to the axis instance you need to double check the code you’re using to set x-ticks, `len(drinks)` is just an integer.

Could you please provide the code that doesn’t work for your second problem. I don’t know what you mean.

Sorry, i wrote wrong in my topic. I actually wrote

``````ax = plt.subplot()
ax.set_xticks(range(len(drinks)))
ax.set_xticklabels(drinks)
``````

In my code, i just corrected in my question this mistake.

So i if want to change the axis, in this case, i should use `ax = plt.gca()`? I didn’t knew this function.

The code for my second question is like this:

``````n = 2  # This is our second dataset (out of 2)
t = 2 # Number of datasets
d = range(len(drinks)) # 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)
``````

I first put it like this, but i got an error and couldn’t follow to the next step in the lesson. I had to count every element on `drinks` list and set `d=6` for a correct answer. I’m wondering if this is a codecademy thing, as in you have to put 6 because the platform is evaluating with the number 6, or if this is a general Matplotlib thing and with a function would never work anyways.

1 Like

My apologies. Whilst I’m fairly familiar with matplotlib I’ve not undertaken the lessons on codeacademy so I don’t know what you have and have not been introduced to.

With the code you had plt.gca() seemed simplest (get current axis). It provides a reference to the current axis. If you’re using multiple figures/axes this could be an issue as exactly which axis is current might not be entirely clear.

It would probably be best to use the subplot first and then use the bar method from that axis instance. Something like the following may have been used-

``````fig = plt.figure()
ax.bar(store1_x, sales1)
``````

You can define d using range this way but this creates a range object. So in your line to create store_2x you’d use it in the following way-

``````d = range(len(drinks))
store2_x = [t * element + w * n for element in d]
``````

In the code you provided you’re passing defining a range object (d) and then passing it to range again in your comprehension which will end up throwing an error.

1 Like

As far as correcting the x-tick position and the x-tick labels, there are a few ways you can do this. One way is directly through pyplot (plt), and also through the subplot(s)/axes feature. Either one works, but they function in different ways. Check out the Matplotlib documentation for more info.

Whichever way you use, setting the x-ticks to `range(len(drinks))` won’t work, because the spots between your two bars are not exactly lined up with 0, 1, 2, 3, etc. In order to find that middle spot, you need to find the average location of your two bars. Try to figure this out on your own. If you can’t, click below to view the sample code:

Finding the x-tick values by calculating the mean
``````x_ticks = [(x1 + x2) / 2 for x1, x2 in zip(store1_x, store2_x)]
``````
Using plt.xticks()
``````plt.xticks(x_ticks, drinks)
``````
Using ax.set_xticks()
``````#First dataset variables up here
f, ax = plt.subplots()
ax.bar(store1_x, sales1)

#Second dataset variables here

ax.bar(store2_x, sales2)
ax.set_xticks(x_ticks)
ax.set_xticklabels(drinks)
``````
2 Likes

Thank you! It worked!