4
$\begingroup$

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

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.