# Multiple line in one plot

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!!!

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()

``````
1 Like

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.

So like the screen before that you get:

``````plt.plot(unrate[0:12]['MONTH'], unrate[0:12]['VALUE'], c='red')
``````

Here c = colors, which is the first position in the list colors.

In the next iteration i = 1, so you get:

```````plt.plot(unrate[12:24]['MONTH'], unrate[12:24]['VALUE'], c='blue')`
``````

Here c = colors, which is the second position in the list colors.

and so forth.

1 Like

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 of 'red'`

3 Likes

Thank you for your explanations! Now I have a clear picture.

Btw, if i want to create multiple plots, then i need to us `subplots(nrows=n, ncols=m)` or `add_plot()`. I get confused about these two and below two.

`plt.plot()`
`axes.plot()`

Thanks again!

3 Likes

Hello, I am a bit confused here, please were do "MONTH’ come from. Our data has two features that is DATE and VALUE. How come the MONTH.

1 Like

Apologies, I have figure this out. I mistakenly skipped a section. Thank You

2 Likes

My solution:

``````plt.figure(figsize=(10,6))
col_list = ['red', 'blue', 'green', 'orange', 'black']
for i in range(1,6):
plt.plot(unrate['DATE'].iloc[12*(i-1):12*i].dt.month, unrate['VALUE'].iloc[12*(i-1):12*i], c=col_list[i-1])
plt.show()``````
2 Likes

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).

1 Like