# Using ColorData function in ListPlot for a third variable

I am trying plot data using a ColorData function to do a gradient of color from blue to red, I used "Rainbow" option, but something is not going well. The color gradients are not following the third variable.

Data example:

tab={{192,35,11},{35,16,31},{50,200,65}}


My code is:

 ListPlot[List /@ Sort[tab[[All, {1, 2}]]], AxesOrigin -> {0, -90},
PlotRange -> {{0, 360}, {-90, 90}}, Frame -> True, FrameLabel -> {"x", ""},
Background -> GrayLevel[.01], PlotStyle -> (PointSize[0.03]),
ColorFunction -> (ColorData["Rainbow"][#/90] & /@ List /@ Tab[[All, 3]]),
AspectRatio -> 1, PlotLegends -> Placed[BarLegend[{"Rainbow", {10, 75}},
LegendMargins -> {{0, 0}, {10, 5}}, LegendLabel -> "[km/seg]",
LegendFunction -> "Panel"], Right], ImageSize -> Large]


In such cases you might want to use Graphics with Blend.

tab = Sort@RandomInteger[100, {20, 3}];

{c1, c2} = MinMax[tab[[All, 3]]]
(*you can define your own minimum or maximum value*)

col[c_] := Blend[{Red, Blue}, (c - c1)/(c2 - c1)]
(*color scaled between 0 and 1. You can choose any combination of colors*)

leg = BarLegend[{{Red, Blue},{c1, c2}},LegendLabel->"[km/seg]",ImageSize -> 200];
(*define the legend*)

Grid[{{Graphics[{{PointSize[Large], col[#[[3]]], Point[#[[1 ;; 2]]]} & /@ tab,
Line[tab[[All, 1 ;; 2]]] (*Line is optional*)},
Frame -> True, ImageSize -> 200], leg}}]


• Now it is more clear scaling data color as you did . ColorData and ColorFunction are not so easy to understand how implementing them. Commented Jul 9, 2017 at 5:17
tab = {{192, 35, 11}, {35, 16, 31}, {50, 200, 65}};
styledtab = Style[{#, #2}, PointSize[.03], ColorData[{"Rainbow", {10, 75}}][#3]] & @@@ tab;

ListPlot[styledtab, AxesOrigin -> {0, -90},
PlotRange -> {{0, 360}, {-90, 250}}, Frame -> True,
FrameLabel -> {"x", ""}, Background -> GrayLevel[.01], AspectRatio -> 1,
PlotLegends -> Placed[BarLegend[{"Rainbow", {10, 75}},
LegendMargins -> {{0, 0}, {10, 5}}, LegendLabel -> "[km/seg]",
LegendFunction -> "Panel"], Right], ImageSize -> Large]


Update: Perhaps BubbleChart is more convenient to use with ColorFunction

BubbleChart[tab, BubbleSizes -> {0.07, .07},
ColorFunction -> (ColorData[{"Rainbow", {10, 75}}][#3] &),
ColorFunctionScaling -> False, AxesOrigin -> {0, -90},
PlotRange -> {{0, 360}, {-90, 250}}, Frame -> True,
FrameLabel -> {"x", ""}, Background -> GrayLevel[.01],
AspectRatio -> 1, ImageSize -> Medium]


• With the black background the frame-axes are hidden (it also looks nice with a white background, +1 anyway)
– eldo
Commented Jul 7, 2017 at 13:26