I have a function that takes a numeric argument and returns a list of numbers. I want to plot each element of the list in a different color.

If I use this command,


all the elements are plotted in the same color. The function takes only a numeric argument (its definition is f[x_?NumericQ]:=...) so I can't use Evaluate like in this question.

So far I've been using this command:


Which works fine (since the function evaluates very fast, I don't mind there are redundant evaluations here, see this question). However this is not very elegant, and considering I have 16 elements to plot, it gets downright ugly.

Is there a more elegant way to plot each element in a different color?

  • 1
    $\begingroup$ How about Plot[Evaluate[f[x]], ...]? $\endgroup$ – wxffles Nov 14 '12 at 19:08
  • 1
    $\begingroup$ @wxffles if you have f[x_?NumericQ]:=... (as indicated in the question) then that doesn't work. $\endgroup$ – acl Nov 14 '12 at 19:19
  • 1
    $\begingroup$ Identical question asked prior to mathematica.SE existence. $\endgroup$ – Sasha Nov 14 '12 at 19:29
  • $\begingroup$ @Sasha - in the question you linked to the problem is to avoid redundant evaluations of the function, since it is expensive to evaluate (just like in this question, which is already mentioned above). In my question I don't mind redundant evaluations, my problem is coding style. These are two different issues. $\endgroup$ – Joe Nov 15 '12 at 8:27
  • $\begingroup$ The two highest voted answers below are effectively duplicates of answers to (8637). For this reason I argue that this question is a duplicate of that one. Please vote to close if you agree or comment here if you do not. $\endgroup$ – Mr.Wizard May 25 '15 at 13:59

This seems to work:

With[{n = Length@f[0]},
  Plot[Evaluate[Hold[f[x][[#]]] & /@ Range[n]], {x, 0, 1}]]
  • 7
    $\begingroup$ Interesting, Hold is invisible to Plot. +1 $\endgroup$ – Rojo Nov 14 '12 at 20:22
  • $\begingroup$ The essence of this method was previously posted here: (8643) $\endgroup$ – Mr.Wizard May 25 '15 at 13:58

PlotStyle settings as functions

With @acl's example function

 f[x_?NumericQ] := {x, x^2, Sin@x, Cos@x, ArcTan[x]}

 i = 1; 
 Plot[f[x], {x, -Pi, Pi}, 
  PlotStyle -> ({Thick, {Red, Green, Blue, Orange,Brown}[[i++]], {##}} &)]

enter image description here

or, a variation:

i = 1;
Plot[f[x], {x, -Pi, Pi}, PlotStyle -> 
 ({Thick, ColorData[5, "ColorList"][[;; Length[f[1]]]][[Mod[i++,Length[f[1]], 1]]], 
     Arrow @@@ {##}} &)]

enter image description here

Update: ... or use DownValues of the function:

 Plot[Evaluate@DownValues[f][[All, 2]], {x, -Pi, Pi},  PlotStyle -> Thick]

enter image description here

  • 2
    $\begingroup$ This is very cool! But I think it fails if your functions get discontinuous. Try with 1/x}. +1 $\endgroup$ – Rojo Nov 14 '12 at 20:14
  • $\begingroup$ @Rojo, this trick is still a new discovery for me, so it has many gaps. I think the case you mention can be adressed partially using something like Mod[i++, Length[f[1]], 1] instead of i++ as the part index, bu then you get the two parts of 1/x colored differently. $\endgroup$ – kglr Nov 14 '12 at 20:26
  • 3
    $\begingroup$ Using the downvalues isn't a robust solution, because it completely disregards additional caveats to the definitions. For example, plot: Clear@f;f[x_?NumericQ] /; x < 0 := -{x, x^2, Sin@x, Cos@x, ArcTan[x]} f[x_?NumericQ] := {x, x^2, Sin@x, Cos@x, ArcTan[x]} $\endgroup$ – rm -rf Nov 15 '12 at 2:08
  • $\begingroup$ @rm-rf, oops -- It did feel too clean to be robust.(I was trying to remove the condition ?NumericQ from the DownValues when I bumped into this.) thanks. $\endgroup$ – kglr Nov 15 '12 at 3:01
  • 2
    $\begingroup$ Unfortunately like Simon's code this no longer works in version 10 or 10.1. $\endgroup$ – Mr.Wizard May 25 '15 at 13:41

Here is a variation of wxffles' method using Indexed rather than undocumented behavior of Hold within Plot. Hold still works in v10.1 but I think this is more likely to remain working; note that sadly kguler's answer no longer works due to changes in undocumented behavior.

f[x_?NumericQ] := {x, x^2, Sin@x, Cos@x, ArcTan[x]}

Plot[Indexed[f[x], #2] & ~MapIndexed~ f[0], {x, -4, 4}, Evaluated -> True]

enter image description here

Alternatives to Indexed are described here:


This is horrendous, but:

f[x_?NumericQ] := {x, x^2, Sin@x, Cos@x, ArcTan[x]}
length = Length@f[0];
  Plot[f[x][[i]], {x, -1, 1}, 
   PlotStyle -> {ColorData["SunsetColors"][i/length]}],
  {i, 1, length}

Mathematica graphics

f[x_?NumericQ] := {x, x^2, Sin@x, Cos@x, ArcTan[x], ArcCos[x], 
  x^3/3, -x + 4/x}

And now

Block[{Plot, Part, x},
 Plot[f[x]~Part~# &~Array~8, {x, 0, 1}]


Plot[If[x \[Element] Reals, f[x][[#]]] &~Array~8, {x, 0, 1}, 
 Evaluated -> True]

Using what @wxffles just showed us in his answer

Plot[Hold@f[x][[#]] &~Array~8, {x, 0, 1}, Evaluated -> True]

Stealing @kguler's idea but impelmenting it more manually

Module[{i = 0}, 
 Plot[f[x], {x, 0, 1}] /. 
  l_Line :> {ColorData[1, "ColorList"][[++i]], l}]
  • $\begingroup$ Infix with part for no good reason $\endgroup$ – Rojo Nov 14 '12 at 20:08

You have to trick mma in thinking that your subsequent datapoints are separate datasets to be able to use PlotStyle with multiple colors.

Dummy data:

vals = Range[0, 2 \[Pi], 1/6 \[Pi]];
results = Sin[#] & /@ vals;

Getting this data in a the right format:

data = Partition[Transpose[{vals, results}], 1];

Then it's just making a rule for the colors and plot the whole thing:

 PlotStyle -> 
   Rule, {Range[Length[vals]], 
    Table[ColorData["Crayola"][[3, 1,RandomInteger[{1, 134}]]],{Length[vals]}]}]]
  • 1
    $\begingroup$ By using ListPlot you're not taking advantage of the adaptive sampling algorithm of Plot. $\endgroup$ – Joe Nov 15 '12 at 8:52

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