3
$\begingroup$

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]]

enter image description here

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.

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

3 Answers 3

2
$\begingroup$
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]

enter image description here

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]

enter image description here

$\endgroup$
4
  • $\begingroup$ Thanks! any idea why the second approach (at least) does not work when replacing Hue[]? by Opacity[]? $\endgroup$
    – Wouter
    Sep 6, 2023 at 8:34
  • $\begingroup$ 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? $\endgroup$
    – Wouter
    Sep 11, 2023 at 3:41
  • $\begingroup$ 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]]]? $\endgroup$
    – kglr
    Sep 11, 2023 at 5:05
  • $\begingroup$ Genius! does the job, thanks $\endgroup$
    – Wouter
    Sep 11, 2023 at 5:27
4
$\begingroup$

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[{
    MapThread[{#1, Line[#2]} &, {Most@cols, linesegs}]
    }
   ];
    
Show[p1, g1]

enter image description here


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 -> {
    MapThread[{#1, Line[#2]} &, {Most@cols, linesegs}]
    }
  ]
$\endgroup$
3
$\begingroup$
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]]

enter image description here

$\endgroup$
1
  • 1
    $\begingroup$ This one is nice in its simplicity $\endgroup$
    – Wouter
    Sep 6, 2023 at 9:11

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.