Range() function to create subplots

https://app.dataquest.io/m/143/multiple-plots/7/comparing-across-more-years

Hello!
This part of the mission explans how to use a for loop to creat multiple plots. There is this code there:

fig = plt.figure(figsize=(12,5))

for i in range(2):
    ax = fig.add_subplot(2,1,i+1)
    start_index = i*12
    end_index = (i+1)*12
    subset = unrate[start_index:end_index]
    ax.plot(subset['DATE'], subset['VALUE'])

plt.show()

Since each loop is assigning the subplot the same variable (ax), I would expect that it would keep only the subplot assigned in the second and last loop, since the plt.show() is outside the loop:

for i in range(2):
    ax = fig.add_subplot(2,1,2)
    start_index = 1*12
    end_index = (1+1)*12
    subset = unrate[12:24]
    ax.plot(subset['DATE'], subset['VALUE'])

plt.show()

My question is: why the plt.show() shows the both plots and not just the second one, since it is the value assigned to the ax variable when the for loop ends?

Regards,
Paulo

1 Like

I’m not sure but I think it’s this line of code that is responsible for the behaviour you’re confused over. With each iteration of the for loop, the subplot index (i+1) changes with each loop. Therefore, you are in fact using different ax objects on each loop.

2 Likes

In this case we are storing more than one subplot in one unique variable?

I think of (2, 1, i+1) as being sort of an address on a plot. It kinda says “hey, I live on block that’s shaped like this: 2 rows, 1 column, house number i+1”

Each time you loop through, the shape of the block stays the same but the “house number” keeps changing. So you’re creating different ax objects that have “the same shape” but a different address.

Also don’t forget this line at the end of the loop: ax.plot(subset['DATE'], subset['VALUE']) is essentially plotting your ax object for the current loop iteration before we “reuse” the variable ax.

That way, when your for loop is done, your ax objects (two of them: index=1, and index=2) have already been plotted and python displays both when you call plt.show()

3 Likes

As always, another perfect explanation from your side, Mike! Once more, thank you for your usual patience.

Please, you are making my head too big! :laughing:

And really, it should be me thanking you for the opportunities to help; they are helping me understand things better too!

So, really, truly, honestly: thank you for asking questions that I feel comfortable enough trying to answer. Not everyone is as confident as you are in asking questions. I appreciate your dedication to learning.

That said, be prepared for it: one of these times, I will explain something incorrectly because I don’t know the material well enough or I have taught myself something incorrectly and then pass on that misinformation to you! I apologize in advance for if/when this happens.

Luckily, the community is filled with far more intelligent people than me. If we step out of line, I feel safe in knowing someone will guide us back to understanding.

(Hint, hint @Sahil, @the_doctor, @otavios.s, @DishinGoyani, @moriturus7 to name just a few…)

4 Likes

You’re doing great, @mathmike314, keep it up!

2 Likes