Problem with x-axis ticks in Jupyter Notebook

Screen Link: https://app.dataquest.io/m/144/bar-plots-and-scatter-plots/5/aligning-axis-ticks-and-labels

My Code:

num_cols = ['RT_user_norm', 'Metacritic_user_nom', 'IMDB_norm', 'Fandango_Ratingvalue', 'Fandango_Stars']
bar_heights = norm_reviews[num_cols].iloc[0].values
bar_positions = arange(5) + 0.75
fig, ax = plt.subplots()
ax.bar(bar_positions, bar_heights, 0.5)
plt.show()

What I expected to happen:

What actually happened: The bar graph plotted successfully but the xlabels are aligned with the right end of the bars. When i run the same code in the Dataquest script.py the x labels where properly aligned at the center of each bar.

image|640x391

Replace this line with the output/error
!

Hi @ramarso91, welcome to the community!

I’m thinking that this is a version issue. The version of matplotlib being used on the DQ platform is 1.5.1, and I’m guessing you might have version 3.1.x on your local machine (I have 3.1.3). You can find out more information about this in this post, and you can use the steps in this post to determine what versions are running on your machine.

I think in the newer versions of matplotlib they made the align parameter have a default of center, so when we do the +0.75 adjustment it’s shifting from the center.

I’ll put these links here for your reference in case you need them later:
Matplotlib 1.5.1 documentation for Axes.bar
Matplotlib latest version for Axes.bar

1 Like

Thank you for your reply, April!

I’ve read the posts you mentioned and resolved the issue by erasing +0.75 from tick_positions (As you mentioned) and setting tick_positions = range(0,5). It seems the first position needs to be 0, otherwise the tick labels won’t correspond to its proper bar, and the last label won’t be in the appreciable range of the axes.

It would be:

bar_positions = arange(5) 
tick_positions = range(0,5)