12

4

I have a function `f`

which takes a number as input, and returns a list of numbers (the length of the list is constant). `f`

is hard to calculate (each evaluation takes a long time).

I want to plot the different components of `f`

in *different colors*.

If I use this command:

```
Plot[f[x], {x, -2, 2}]
```

all the lines are drawn in the same color.

If I use this command:

```
Plot[{f[x][[1]], f[x][[2]], f[x][[3]]}, {x, -2, 2}]
```

(assuming the list has three components) the lines are drawn in different colors, but the function is called three times the necessary amount.

Note that this is a numeric function, it cannot be evaluated with a symbolic argument (i.e. the function definition begins with `f[x_Real]:=`

), so there is no use in using `Evaluate`

like in this question.

5The following code suggests that even

`Plot[f[t],...]`

evaluates`f`

multiple times.`f[x_Real] := (i++; {x, x + 1, x + 2})`

and`i = 0; Plot[f[t], {t, 0, 1}, PlotPoints -> 10, MaxRecursion -> 0]`

, then`Print[i]`

. The result is 31, not 10-ish. So, solutions just using`Plot`

may not work as well. – Yu-Sung Chang – 2012-06-05T12:47:03.710@Yu-SungChang, sorry, I didn't see your comment. I wrote an answer which basically states the same. I give you +1 for your comment ;-) – halirutan – 2012-06-05T13:04:24.720

Relevant SO question: "How to select the “best” new point when sampling a near-parabolic function?"

– Alexey Popkov – 2012-06-07T08:33:23.260Another related SO question: "Telling Plot to style vector-valued black-box functions in Mathematica"

– Joe – 2012-11-15T08:29:52.220