Why do you make a subplot instead of a plot here?

Screen Link:

My Code:

fig, ax = plt.subplots()
ax.plot(women_degrees['Year'], women_degrees['Biology'], c='blue', label='Women')
ax.plot(women_degrees['Year'], 100-women_degrees['Biology'], c='green', label='Men')

ax.tick_params(bottom="off", top="off", left="off", right="off")
ax.set_title('Percentage of Biology Degrees Awarded By Gender')
ax.legend(loc="upper right")



In assignment 147.6 I am trying to understand the logic behind the code. I have two questions about this:

  1. Why do we use a subplot plt.subplots() instead of a figure plot ( plt.figure())? In the previous assignment, we did the opposite (https://app.dataquest.io/m/147/improving-plot-aesthetics/4/visualizing-the-gender-gap )
  2. When do we use ax.set_title in stead of plt.title ?
1 Like

Hi @jeroenstikkelorum ,

Because in this mission screen we’re practicing the Axes.tick_params() method, hence you need to create an ax (an instance of the Axes class).

Actually, the rest of the code, apart from the lines where you use this Axes.tick_params() method, and instantiate the Axes class in fig, ax = plt.subplots(), can remain the same as in that mission screen, i.e. you can use plt.title also here, as well as plt.plot. I mean, the code below would also work perfectly:

fig, ax = plt.subplots()
plt.plot(women_degrees['Year'], women_degrees['Biology'], c = 'blue', label = 'Women')
plt.plot(women_degrees['Year'], 100 - women_degrees['Biology'], c = 'green', label = 'Men')
ax.tick_params(bottom = 'off', top = 'off', left = 'off', right = 'off')
plt.title('Percentage of Biology Degrees Awarded By Gender')
plt.legend(loc = 'upper right')