HELP Code Question : Star Wars Survey

Hey guys,

I am currently finishing the Step 2 of the course doing the Star Wars Survey. I decided to create a for loop in order to automatize the creation of charts. My code looks like this:

## Rankings

categories = [males, females,star_trek_fan, star_trek_not_fan, sw_fan, sw_not_fan]
categories_names = ['males', 'females','star_trek_fan', 'star_trek_not_fan', 'sw_fan', 'sw_not_fan']

fig, axes = plt.subplots(nrows = 3, ncols= 2, figsize=(15,15))
fig.subplots_adjust(hspace=0.2, wspace = 0.2)
fig.suptitle('Ranking of Means', size = 20)

for category, sp, category_name in zip(categories, range(0,6), categories_names):  
    
    rankings = [col for col in category if col.startswith('ranking')]
    ranking_means = category[rankings].mean()
    
    y_pos = np.arange(len(rankings))
    
    ax = fig.add_subplot(3, 2, sp+1)

    
    ax.bar(rankings, ranking_means, align='center', alpha=0.5)

    ax.tick_params(bottom="off", top="off", left="off", right="off")
    
    ax.set_title(category_name)


## Watched movies


categories = [males, females,star_trek_fan, star_trek_not_fan, sw_fan, sw_not_fan]
categories_names = ['males', 'females','star_trek_fan', 'star_trek_not_fan', 'sw_fan', 'sw_not_fan']

fig, axes = plt.subplots(nrows = 3, ncols= 2, figsize=(15,15))

fig.subplots_adjust(hspace=0.2, wspace = 0.2)
fig.suptitle('Most Watched Movies', size = 20)

for category, sp, category_name in zip(categories, range(0,6), categories_names):  
    
    seen = [col for col in category if col.startswith('seen')]
    seen_movies = category[seen].sum()
    
    y_pos = np.arange(len(seen))
    
    ax = fig.add_subplot(3, 2, sp+1)



    ax.bar(seen, seen_movies, align='center', alpha=0.5)

    ax.tick_params(bottom="off", top="off", left="off", right="off" )
    
   
    ax.set_title(category_name)

The thing is that with the graphs created, I can’t hide the numbers in the axis. Let me show you through a picture:

As you can see, the axis have both the numbers and the labels and I don’t know how to hide them.

Could you help me with this?

Thank you!

I played around with it and ultimately ended up doing 2 things for both sets of figures that seemed to clean it up.

  1. add sharex=True and sharey=True parameters to fig, axes
  2. added code to remove ticks with plt.xticks and plt.yticks

Here’s the snippet that shows the changes for the first set.

fig, axes = plt.subplots(nrows = 3, ncols= 2, figsize=(15,15), sharex=True, sharey=True)
fig.subplots_adjust(hspace=0.2, wspace = 0.2)
fig.suptitle('Ranking of Means', size = 20)
plt.xticks([])
plt.yticks([])

These were the results I got when I made these changes. I don’t know why some of the labels on the ranking go by 1s or 0.5s though.


3 Likes

@april.g you made my day. Thank you a lot! :slight_smile:

1 Like