# Custom color in data points

Let's create some random data

Clear["Global*"];

n = 10^4;
Nmax = 10000;
data = Table[{RandomReal[{-1, 1}], RandomReal[{-1, 1}],
RandomInteger[{0, 500}]}, {i, 1, n}];


then the so-called Jet color function

jet[u_?NumericQ] :=
Blend[{{0, RGBColor[0, 0, 9/16]}, {1/9, Blue}, {23/63,
Cyan}, {13/21, Yellow}, {47/63, Orange}, {55/63, Red}, {1,
RGBColor[1/2, 0, 0]}}, u] /; 0 <= u <= 1;


and finally plot the points, according to the value of the third column

Nmin = 100;
Nmax = 400;
valrange = {Nmin, Nmax};
data[[All, 3]] = Rescale[data[[All, 3]] // N, valrange];

colfunc[x_] := jet[x[]];
g0 = Graphics[{PointSize[0.01],
Point[#[[1 ;; 2]], VertexColors -> colfunc[#]] & /@ data}] As we can see, many of the points are plotted in black. This is because the third element of the list runs from 0 to 500, while after the rescale we color only those between 100 and 400.

I want the following: All points for which the third element in less than 100 should be in gray, while all points for which the third element is above 400 should be in darker red.

Any suggestions? That is, the values below min are mapped to negative values and those above max are mapped to values greater than 1.

So you can modify your colfunc to change the colors so that (1) if the rescaled value of the third column is negative we have a Gray point, (2) if the rescaled value of the third column is no less than 1 we have a Darker@Red point, else (3) the point is colored using jet:

colfunc[x_] := Piecewise[{{Gray, # <= 0}, {Darker@Red, # >= 1}}, jet @ #] &@x[];

g0 = Graphics[{PointSize[0.01], Point[#[[1 ;; 2]], VertexColors -> colfunc[#]] & /@ data}] Update: a more flexible approach that allows one to specify the thresholds for rescaling and re-coloring:

ClearAll[colfunc2]
colfunc2[t1_: {100, 400}, t2_: {100, 400}][x_] :=
Piecewise[{{Gray, # <= Rescale[t1[], t2]}, {Darker@Red, # >=
Rescale[t1[], t2]}}, jet@#] &@x[];

g0 = Graphics[{PointSize[0.01], Point[#[[1 ;; 2]],
VertexColors -> colfunc2[][#]] & /@ data}] g1 = Graphics[{PointSize[0.01], Point[#[[1 ;; 2]],
VertexColors -> colfunc2[{200, 250}][#]] & /@ data}] Update 2:

assign a custom color (let's say magenta) to those for which the third element is between 200 and 250.

colfunc3[x_] :=  Piecewise[{{Gray, # <= 0}, {Darker@Red, # >= 1},
{Magenta, Rescale[200, valrange] <= # <= Rescale[250, valrange]}}, jet@#] &@x[]

Graphics[{PointSize[0.01],
Point[#[[1 ;; 2]], VertexColors -> colfunc3[#]] & /@ data}] • Nice and elegant! Let's wait for a while (before accepting the answer), in case someone else wants to propose an alternative. – Vaggelis_Z Jan 3 '18 at 19:23
• Just of curiosity: if we would like to plot all points for which the third element is between 200 and 250, what should be changed to the piecewise function? – Vaggelis_Z Jan 3 '18 at 19:27
• If you want to completely strip out points I'd separately use select ( Select[data,200<#[]<250&] ) rather than trying to make them transparent or something. – george2079 Jan 3 '18 at 19:37
• @Vaggelis_Z, if mean to plot only points for which the third element is between 200 and 250 and drop the rest, then you can use Select[data, 200<= #[]<=250&] or Pick[data, 200<= #[]<=250&/@data] without making any chnges to the color function. Or, perhaps, modify the colfunc to replace 0 with Rescale[200, valrange] and 1 with Rescale[250, valrange]? – kglr Jan 3 '18 at 19:38
• No, I mean to plot all the points but assign a custom color (let's say magenta) to those for which the third element is between 200 and 250. All the rest of them should be according to your answer. – Vaggelis_Z Jan 3 '18 at 19:41
Clear["Global*"];

n = 10^4;
Nmax = 10000;
SeedRandom;
data = Table[
{RandomReal[{-1, 1}], RandomReal[{-1, 1}], RandomInteger[{0, 500}]},
{i, 1, n}];

dataGroups = {
Select[data, #[] < 100 &],
Select[data, 100 <= #[] <= 400 &],
Select[data, #[] > 400 &]};

jet[u_?NumericQ] :=
Blend[{{0, RGBColor[0, 0, 9/16]}, {1/9, Blue}, {23/63,
Cyan}, {13/21, Yellow}, {47/63, Orange}, {55/63, Red}, {1,
RGBColor[1/2, 0, 0]}}, u] /; 0 <= u <= 1;

Nmin = 100;
Nmax = 400;
valrange = {Nmin, Nmax};
dataGroups[[2, All, 3]] =
Rescale[dataGroups[[2, All, 3]] // N, valrange];

colfunc[x_] := jet[x[]];
g0 = Graphics[{PointSize[0.01],
Point[#[[1 ;; 2]], VertexColors -> colfunc[#]] & /@ dataGroups[],
Gray, Point[#[[1 ;; 2]] & /@ dataGroups[]],
Darker[Red], Point[#[[1 ;; 2]] & /@ dataGroups[]]}]
` 