ColorFunction specifies that for ListLinePlot it takes the $x,y$ data as inputs. I, however, would like to take the array index (or in general some external array of the same length) as the input for ColorFunction so that, if we use a rainbow color scheme, the earlier points show up as purple while the last points show up as red. An application would be visualisation of the long time behaviour of some system when data is imported from an external source.

For example if we take

ListLinePlot[Table[E^-0.001 x {Cos[x], Sin[x]}, {x, 0, 100, 0.1}]]

then the outer lines should be purple ending up as red as the plot spirals in.

EDIT: I am now convinced the best thing you can do if you import a large amount of time ordered data into MMA is to immediately convert that data into an interpolation function and then use ParametricPlot. Here is an example:

data = Table[{Sin[2 u], Cos[u]}, {u, 0, 100, 0.1}];
indexlist = Rescale[Range@Length@data] (*or your external list to control color*);
iF = Interpolation[MapThread[{#, #2} &, {indexlist, data}], InterpolationOrder -> 1];
ParametricPlot[iF[u], {u, 0, 1}, ColorFunction -> Function[{x, y, u}, ColorData["Rainbow"][u]]] 

This answer is obviously based on @kglr's answer but the slight difference is he uses interpolation to return a color as a function of ${x,y}$ position while here interpolation is used to return ${x,y}$ as a function of time. In this way, if you have overlapping data as I do above, the later data is always plotted on the top.


3 Answers 3

data = Table[E^-0.001 x {Cos[x], Sin[x]}, {x, 0, 100, 0.1}];

indexlist = Rescale[Range @ Length @ data] (* or your external list to control color*);


iF = Interpolation[MapThread[{#, #2} &, {data, indexlist}], InterpolationOrder -> 1];

 AspectRatio -> 1, 
 ColorFunctionScaling -> False, 
 ColorFunction -> (ColorData[{"Rainbow", "Reversed"}][ iF[#, #2]]&)]

enter image description here

Alternative methods:


PolarPlot[E^-0.001 x , {x, 0, 100}, 
 ColorFunction -> (ColorData[{"Rainbow", "Reversed"}][#3] &)

same picture


ParametricPlot[E^-0.001 x {Cos[x], Sin[x]}, {x, 0, 100}, 
 ColorFunction -> (ColorData[{"Rainbow", "Reversed"}][#3] &)]

same picture

Graphics + VertexColors

Graphics[Line[data,  VertexColors -> (ColorData[{"Rainbow", "Reversed"}] /@ indexlist)], 
  Axes -> True]

same picture

  • $\begingroup$ This is a good answer, but I think you want (ColorData[{"Rainbow", "Reverse"}] to satisfy the OP's coloring order. $\endgroup$
    – m_goldberg
    Sep 13, 2019 at 14:14
  • 1
    $\begingroup$ Thank you @m_goldberg. ColorData[{"Rainbow", "Reversed"}] and ColorData[{"Rainbow", "Reverse"}] both work. $\endgroup$
    – kglr
    Sep 13, 2019 at 14:18
  • $\begingroup$ You're right, they both work. Your solution is independent of the ordering of the Rainbow color scheme. Still, I feel a little stupid about making my original comment. $\endgroup$
    – m_goldberg
    Sep 13, 2019 at 14:44
  • $\begingroup$ I hesitated to mark this as the accepted answer, although it more than answers my initial question, since I was worried what happens if you have data that overlaps. suppose data = Table[{Sin[2 u], Cos[u]}, {u, 0, 100, 0.1}]; the older colors are plotted first and obscure the newer ones. The quick and dirty workaround is to simply Reverse data in ListLinePlot but I think if you are importing data the best thing to do is turn it into a parametric function and then plot based on time. Of course I explicitly asked for ListLinePlot but I think this method is better. $\endgroup$
    – Takoda
    Sep 19, 2019 at 7:37

I think it is easier to generate the plot you want with ParametricPlot Like so:

With[{n = 100},
  ParametricPlot[E^-0.001 x {Cos[x], Sin[x]}, {x, 0, n},
    Mesh -> n - 1,
    MeshStyle -> Transparent,
    MeshShading ->Table[ColorData[L{"Rainbow", "Reverse"}][i/n], {i, n}]]]


With[{lines=Line/@Partition[Table[E^-0.001 x {Cos[x], Sin[x]}, {x, 0, 100, 0.1}],2,1]}, 
Frame -> True]]

enter image description here


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.