4
$\begingroup$

Suppose I have a simple function like $\cos(kx)$, and I am plotting a number of such functions with different values for $k$ as in:

Plot[Table[Cos[k x], {k, 1, 4, 1}], {x, 0, π}]

How can I color them depending on the $k$ parameter without setting it manually myself, as in:

Plot[Evaluate[Table[Cos[k x], {k, 1, 4, 1}]], {x, 0, π},
  PlotStyle -> Table[Blend[{Blue, Red}, k/4], {k, 1, 4, 1}]]

?

$\endgroup$
6
  • $\begingroup$ Plot[Evaluate[Table[Cos[k x], {k, 1, 4, 1}]], {x, 0, \[Pi]}] automatically colors them differently for each k. Is that what you mean? If you want each plot colored specifically but you don't want to specify the colors "manually," then either I don't understand what you want or it cannot be accomplished. $\endgroup$
    – Michael E2
    Sep 25, 2017 at 23:36
  • 3
    $\begingroup$ Maybe this?: Plot[Evaluate[Table[Style[Cos[k x], Blend[{Blue, Red}, k/4]], {k, 1, 4, 1}]], {x, 0, \[Pi]}] $\endgroup$
    – Michael E2
    Sep 25, 2017 at 23:38
  • $\begingroup$ @MichaelE2 Yes, that's exactly what I wanted! You got it! I'm gonna study that syntax. $\endgroup$ Sep 25, 2017 at 23:40
  • 1
    $\begingroup$ I was about to suggest Style as @Michael just posted; see (8191) $\endgroup$
    – Mr.Wizard
    Sep 25, 2017 at 23:42
  • $\begingroup$ @Michael Do you think this is close enough to 8191 to be marked as a duplicate? $\endgroup$
    – Mr.Wizard
    Sep 25, 2017 at 23:51

2 Answers 2

6
$\begingroup$

This seems to be the desired form:

Plot[
 Evaluate[Table[Style[Cos[k x], Blend[{Blue, Red}, k/4]], {k, 1, 4, 1}]],
 {x, 0, π}]

Mathematica graphics

$\endgroup$
1
$\begingroup$

It may help to use PlotLabels and PlotLegends options to identify the plotted functions.

Plot[Evaluate[
Table[Style[Cos[k x], Blend[{Blue, Red}, k/4]], {k, 1, 4, 1}]], {x, 0, π}, 
PlotLabels -> Placed[Automatic, {Above, Below, After, Below}], 
PlotLegends -> "Expressions"]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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