Multiplying by 12

Hello,
this is an exericse from course"exploratory data visualization". This question been posted in FAQ but nobody answered. (in 2018)
Can anyone explain me why we are multiplying i*12 and (i+1)*12?
Thanks for explanation.
Roman

fig= plt.figure(figsize=(12,12))
for i in range(5):
ax = fig.add_subplot(5,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()

Hello @roumenko! :wave: Welcome to the forum! I am sorry that this has remained unanswered in the FAQ.

I will do my best to explain the most clearly possible here.

So, we want to plot 5 years worth of data. We know, from previous steps in the mission and from looking at the data set that each year spans 12 rows, each row containing unemployment information for a month of that year.

As such, the first 12 rows, those at index 0 to 11 contain information about the first year, the next 12 ones, indexes 12 to 23 information about the second year, the next 12, indexes 24 to 35 info about the 3rd year and so on…

Now, because we are using a for loop to make these plots we have to find a way to dynamically retrieve the subset of the data set that corresponds to each year. The variable i comes in very handy. It takes values in the range(0, 5) so it will be: 0, 1, 2, 3, 4.

For the plot of the first year, i=0 and the subset of the data set that interests me is indexes 0 to 11. So, I could find the starting index by doing: i * 12 = 0 * 12 = 0 which will give me 0 and the ending index to be used in the slicing by doing (i + 1) * 12 = (0 + 1) * 12 = 1 * 12 = 12. So I will be slicing the data set starting at 0 and ending at 12 to get all items at indexes 0 till 12, the one at index 12 not being included.

This works out great and holds for every subset. For instance, when i = 1 the i * 12 = 12 and (i + 1) * 12 = 24 and so on…

Does that make some more sense? Please let me know if any part of this is confusing.

2 Likes