Can someone explain the for loop on Multiple Plots?

Screen Link:

My Code:

fig = plt.figure(figsize=(12,5)) #understand this, - basically assigning the size of the chart

for i in range(2): # I dont quite understand what the loop is doing here
    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'])

Hi lrosale1

Here the range() function creates a number sequence of 0, 1
Hence for the first iteration i=0, and for the second iteration. we have i=2.

The function add_subplot(nrows, ncols, index) will create an Axes object and add it to the figure fig, which is a container, to hold all our plots.

gives us
ax = fig.add_subplot(2,1,1) (for first iteration, i=0)
ax = fig.add_subplot(2,1,2) (for second iteration, i=1)

So, we’ll have a plot with 2 rows by 1 column (nrows=2, ncols=1); and the plot will have 2 subplots (1 and 2).

For the first iteration,
start_index will be 0
end_index will be 12
Hence, unrate[0:12], i.e. the first 12 rows will be assigned to subset

Thus, subplot 1 will depict, unrate[0:12]['DATE'] on the x-axis, and unrate[0:12]['VALUE'] on the y-axis.

For the second iteration,
start_index will be 12
end_index will be 24
Hence, unrate[12:24] will be assigned to subset

Thus, the second subplot will depict, unrate[12:24]['DATE'] on the x-axis, and unrate[12:24]['VALUE'] on the y-axis.

Hope it helps.