Bar Plots And Scatter Plots

I run the code below and why it doesn’t show the y-axis tick labels? If I move ax.set_yticklabels(num_cols) below ax.set_yticks(tick_positions) then it gives me the right answer, why’s that?

import matplotlib.pyplot as plt
from numpy import arange
num_cols = ['RT_user_norm', 'Metacritic_user_nom', 'IMDB_norm', 'Fandango_Ratingvalue', 'Fandango_Stars']

bar_widths = norm_reviews[num_cols].iloc[0].values
bar_positions = arange(5) + 0.75
tick_positions = range(1,6)

ax.set_yticklabels(num_cols)


fig,ax = plt.subplots()                       
ax.barh(bar_positions,bar_widths,0.5)
ax.set_yticks(tick_positions)

ax.set_ylabel("Rating Source")
ax.set_xlabel("Average Rating")
ax.set_title("Average User Rating For Avengers: Age of Ultron (2015)")

plt.show()

Hi @gaoweidong999: num_cols is a list that you defined. Thus, ax.set_yticklabels(num_cols) returns the index of each element in the list (i.e. from 0 to 5 as indicated in your actual submission). However, ax.set_yticks(tick_positions) returns the individual element at the index, which is iterated using the range(1, 6).

Like in this example, printing a list with the range() function returns a list of the elements and not the index values.

Thus, you use ax.set_yticks(tick_positions) instead of ax.set_yticklabels(num_cols).

Hope this clarifies!

Thanks for your reply, but I figured out the reason I got the wrong answer was that I ran the codeax.set_yticklabels(num_cols) before ax was definedfig,ax = plt.subplots()

1 Like