Skip to main content

color - Time ColorFunction


I have the following data


data = {{0, 0}, {-0.0385, 0.0667}, {-0.0566, 0.1283}, {0.0966, 
0.0441}, {-0.1166,
0.0038}, {-0.2046, -0.0673}, {-0.2536, -0.21}, {-0.5485, \
-0.2643}, {-0.3702, -0.1148}, {-0.4201, -0.2319}, {-0.5558, -0.2748}, \
{-0.693, -0.3272}, {-0.6276, -0.3844}, {-0.6305, -0.3448}, {-0.671, \

-0.4833}, {-0.6809, -0.3441}, {-0.7082, -0.2548}, {-0.5978, -0.3126}, \
{-0.4337, -0.3588}, {-0.2542, -0.4139}, {-0.3318, -0.3318}, {-0.4991, \
-0.2123}, {-0.4195, -0.1966}, {-0.4797, 0.0285}, {-0.2198,
0.0965}, {-0.1914, 0.1604}, {-0.0854, 0.1668}, {0.0715,
0.3025}, {0.0847, 0.3116}, {0.175, 0.3152}, {0.0123,
0.4577}, {0.0941, 0.4445}, {0.083, 0.3729}, {0.1251,
0.1414}, {0.0239, 0.3119}, {-0.0665, 0.3748}, {-0.0171,
0.4261}, {0.0781, 0.4031}, {-0.0945, 0.315}, {-0.2497,
0.3892}, {-0.285, 0.3823}, {-0.2873, 0.4343}, {-0.196,
0.4628}, {-0.2812, 0.5166}, {-0.186, 0.2917}, {-0.3101,

0.3047}, {-0.4103, 0.2439}, {-0.4335, 0.2251}, {-0.5113,
0.4149}, {-0.6487, 0.4662}, {-0.5045, 0.1807}};

which represents movement of some particle in time 0,...,50.


Using ColorFunction is possible to get rainbow color according to axis $y$ as


ListLinePlot[data,ColorFunction->Function[{x,y},ColorData["Rainbow"][y]],PlotStyle->Thick]

or axis $x$ as


ListLinePlot[data,ColorFunction->Function[{x,y},ColorData["Rainbow"][x]],PlotStyle->Thick]

Is it possible to use ColorFunction according to time 0,...,50 and to get rainbow according to its movement in this time?



Answer



It seems that the links that I've provided are closely related but not duplicates. I decided to write this answer but I will add an extension which may be useful for others.



This is what OP finds well suited to his needs:


Graphics[{Thick, 
Line[data,
VertexColors -> Array[Blend["Rainbow", #/Length@data] &, Length@data]]}]

However one may want to blend the palette along the lines but scalled with path not with number of vertex as in the former example.


path = {0} ~ Join ~ Accumulate[EuclideanDistance @@@ Partition[data, 2, 1]];

Graphics[{Thick,
Line[data,

VertexColors -> Array[Blend["Rainbow", path[[#]]/Last@path] &, Length@data]]}]

Comments