# How to access the index of a list for the colorfunction of a listplot?

From the documentation of ListPlot,

ListPlot[Table[Style[{Cos[t], Sin[2 t]}, Hue[t/(2 Pi)]], {t, 0, 2
Pi, Pi/20}],PlotStyle -> PointSize[Medium]]

Varies the color of the plotted points based on their position in the list, ie the parametric 'time' variable t.

However,

ListPlot[Table[Style[{Cos[t], Sin[2 t]}, Hue[t/(2 Pi)]], {t, 0, 2
Pi, Pi/20}], Joined -> True, PlotStyle -> PointSize[Medium]]

keeps the color of the 'joined' fixed. Is there a way to make it vary too? I know of 'Colorfunction', but according to the documentation, it can in the case of a Listplot only be defined as function of x and y, not of 't'. Im fine with either through interpolation, or just keeping the color of the previous datapoint.

• Please define Nt, Np, lw, Y.
– Syed
Aug 28, 2023 at 3:19
• @Syed have done so Aug 28, 2023 at 3:33
• I mean, please provide definitions as well as a minimal example; such that when executed, this code produces a plot. Thanks.
– Syed
Aug 28, 2023 at 3:37
• @Syed significantly simplified Aug 28, 2023 at 11:59

coords = Table[{Cos[t], Sin[2 t]}, {t, 0, 2 Pi, Pi/20}];

### 1. GraphicsComplex + VertexColors

Graphics[
GraphicsComplex[
coords,
{AbsolutePointSize[7], AbsoluteThickness[2],
Point[Range@Length@coords, VertexColors -> Automatic],
Line[Range@Length@coords, VertexColors -> Automatic]},
VertexColors -> Table[Hue[t/(2 Pi)], {t, 0, 2 Pi, Pi/20}]],
Axes -> True]

### 2. MeshStyle + VertexColors

meshStyle = {AbsoluteThickness[2],
# /. Point[x : {_, __}] :>
Through[{Line, Point}[x,
VertexColors -> Map[Hue]@Subdivide[-1 + Length[x]]]]} &;

ListPlot[coords,
PlotStyle -> AbsolutePointSize[7],
Joined -> True,
Mesh -> All,
MeshStyle -> meshStyle]

• Thanks! any idea why the second approach (at least) does not work when replacing Hue[]? by Opacity[]? Sep 6, 2023 at 8:34
• I figured that I can replace Hue by Function[{o},Directive[Red,Opacity[o]]] for example, but the problem is it still plots the original Joined->true lines in Blue too, which thus become visible. I can set these to white using PlotStyle, but don't see how to make them transparent or make them disappear without affecting the Mesh too? Sep 11, 2023 at 3:41
• something like meshStyle = {AbsoluteThickness[2], # /. Point[x : {_, __}] :> {LineOpacity -> 1, Through[{Line, Point}[x, VertexColors -> Map[Opacity[#, Hue@#] &]@Subdivide[-1 + Length[x]]]]}} &; ListPlot[coords, Joined -> True, Mesh -> All, MeshStyle -> meshStyle, PlotStyle -> Directive[LineOpacity -> 0, AbsolutePointSize[7]]]?
– kglr
Sep 11, 2023 at 5:05
• Genius! does the job, thanks Sep 11, 2023 at 5:27

One way could be the following. This requires that points be relatively close to one another or the plot will have jagged lines.

Clear["Global`*"]
cols = Table[Hue[t/(2 π)], {t, 0, 2 π, π/20}];
segs = Table[{Cos[t], Sin[2 t]}, {t, 0, 2 π, π/20}];
linesegs = Partition[segs, 2, 1];
Dimensions /@ {Most@cols, linesegs}

{{40}, {40, 2, 2}}

p1 = ListPlot[
Table[Style[{Cos[t], Sin[2 t]}, Hue[t/(2 π)]], {t, 0,
2 π, π/20}], PlotStyle -> PointSize[Medium]];

g1 = Graphics[{
}
];

Show[p1, g1]

Or put contents of g1 inside an Epilog statement (same result):

p2 = ListPlot[
Table[Style[{Cos[t], Sin[2 t]}, Hue[t/(2 π)]], {t, 0,
2 π, π/20}], PlotStyle -> PointSize[Medium]
, Epilog -> {
}
]
pts = Table[
Style[{{Cos[t], Sin[2 t]}, {Cos[t + Pi/20], Sin[2 (t + Pi/20)]}}, Hue[t/(2 Pi)]],
{t, 0, 2 Pi, Pi/20}];

Show[ListPlot[pts], ListLinePlot[pts]]

• This one is nice in its simplicity Sep 6, 2023 at 9:11