I found the chapter of multiple plot is confusing. Instructions do not explain the code in detail.
Could someone help to interpret the below code? especially the loop part, what does i*12 for? Thank you a lot!!!
Screen Link: https://app.dataquest.io/m/143/multiple-plots/9/adding-more-lines
My Code:
fig=plt.figure(figsize=(10,6))
colors = ['red', 'blue', 'green', 'orange', 'black']
for i in range(5):
start_index=i*12
end_index=(i+1)*12
subset=unrate[start_index:end_index]
plt.plot(subset['MONTH'],subset['VALUE'],c=colors[i])
plt.show()
range(5) creates a list from 0 to 4 to iterate through. So on the first iteration i is equal to 0, so the starting index is 0*12 = 0 and the end index is (0+1) * 12 = 12.
You want to create multiple plots on the same graph (since you did not specify subplots(nrows=n, ncols=m). How do you want to do this. You want to plot 12 months interval. So you create a subset of the full data.
First you make the start and end indexes for your subsets using
for i in range(5):
start_index=i*12
end_index=(i+1)*12
When i = 0, the start_index and end_index are 0 and 12 respectively. When i=1 the start_index and end_index are 12 and 24 respectively.
The index of your data frame are numbers so you can get subsets of your data using: subset=unrate[start_index:end_index] and you plot each subset using plt.plot(subset['MONTH'],subset['VALUE'],c=colors[i]).
You are plotting Month column against the Value column on each subset. When you set color c=colors[i], you interate over this list colors = ['red', 'blue', 'green', 'orange', 'black'] and you pick colors for the plots according to its index on the colors list. Therefore the first plot with i = 0 will have a colors[0] of 'red'
Hello @eryk.mazus! Your solution is working but it may be a bit confusing since you do not define separate variables for start and stop indexes. You can comment your code to explain what it does and separate each column and colors (DATE, VALUE and cols on three lines).