# Assign color to gridlines automatically

I have a set of equations and I plotted them as follows. I want to plot gridlines for each equation accordingly and assign the same color to the gridline in the same order as the set of the functions. How should I do this?

y = {x, 2 x, 3 x, 4 x};
gridlines = {1, 2, 3, 4};
Plot[Evaluate@Table[y[[i]], {i, 1, 4}], {x, 0, 4},
GridLines -> {gridlines, None}]

• Is this a minimal example of what you seek? Do we really need to know all the specifics of these equations? – David G. Stork Sep 6 '16 at 8:02
• @DavidG.Stork Changed it. – yangyang Sep 6 '16 at 8:10
• So you want the first gridline Blue, then Yellow etc? – Feyre Sep 6 '16 at 8:13
• @Feyre MMA gives colors to function y automatically and I want the same color to the corresponding gridlines. – yangyang Sep 6 '16 at 8:14

y = {x, 2 x, 3 x, 4 x};
gridlines = {1, 2, 3, 4};


Define the gridline function, works for up to 15 functions in y.

gl = Transpose[{gridlines,
Table[ColorData[97, "ColorList"][[i]], {i, Length[gridlines]}]}];


Plot

Plot[y, {x, 0, 4},
GridLines -> {gl, None}] 