1
$\begingroup$

This question is related to this one or that.

SeedRandom[1];

data = RandomInteger[100, {100, 3}];

x = data[[All,1]];
y = data[[All,2]];
z = data[[All,3]];

I want to use ListPlot to show x versus y as points whereby the color should represent z.

How can I use a certain color map like TemperatureMap or my own color table?

(* e.g. own color table *)
number = Length@z; 
colTable = {Table[{Blend[{Blue, Green, Yellow, Red}, i]}, {i, 1/number, 1, 1/number}]}; 
colTable = Flatten[colTable];
$\endgroup$
2
$\begingroup$

Blatantly stealing three-quarters of @ubpdqn's answer (particularly the rescaled ColorFunction), you can use Style to get it working in ListPlot, as requested by the OP:

SeedRandom[1];
data = RandomInteger[100, {100, 3}];

cf = ColorData["TemperatureMap", Rescale[#, MinMax[data[[All, 3]]]]] &;
Row[{ListPlot[Style[{#[[1]], #[[2]]}, cf[#[[3]]]] & /@ data, 
   PlotStyle -> PointSize[Large], ImageSize -> 350], 
  BarLegend[{cf[#] &, {0, 100}}, Range[0, 100, 10]]}]

enter image description here

And, of course, you can change out "TemperatureMap" for any other colour scheme.

$\endgroup$
2
$\begingroup$

In such cases, I go with Graphics.

data = RandomInteger[100, {100, 3}];

{z1, z2} = MinMax[data[[All,3]]];
Grid[
{{Graphics[{PointSize[Large], Hue[(#[[3]] - z1)/(z2 - z1)/2 + 0.5], 
   Point[#[[1 ;; 2]]]} & /@ data, Frame -> True],
  BarLegend[{Hue, {z1, z2}}]}}]

enter image description here

With your custom colormap

clfun[x_] := Blend[{Blue, Green, Yellow, Red}, x]
{z1, z2} = MinMax[data[[All,3]]];
Grid[
{{Graphics[{PointSize[Large], clfun[(#[[3]] - z1)/(z2 - z1)/2 + 0.5],
    Point[#[[1 ;; 2]]]} & /@ data, Frame -> True],
 BarLegend[{clfun[#] &, {0, 1}}, Ticks -> {{0, z1}, {1, z2}}]}}]

enter image description here

To use a TemperatureMap

clfun[x_] := ColorData["TemperatureMap"][x]
$\endgroup$
  • $\begingroup$ This is great, thanks a lot ... how can I to use ListPlot because there I can add PlotRange and other keywords which Graphics does not have. For the solution of the custom colormap I get the info: "Ticks is not an option of BarLegend". Is it possible to access the colors of TemperatureMap? $\endgroup$ – mrz Oct 18 '17 at 9:56
  • $\begingroup$ You can use PlotRage with Graphics. Ticks appears in red with BarLegend but it works. Which version you are using. There might be some problem with old versions. $\endgroup$ – Sumit Oct 18 '17 at 10:05
  • $\begingroup$ Thanks for the solutions with the color map. I am using 11.2 -> PlotRange is possible. I thought that ListPlot is usally used to plot data intead of using Graphics? $\endgroup$ – mrz Oct 18 '17 at 10:32
  • $\begingroup$ well, deep inside ListPlot is a Graphics ;). You are right, it is more convenient to use ListPlot or ListLinePlot while plotting data due to its inbuilt options. You can do the same thing using Graphics but it takes more efforts and commands. $\endgroup$ – Sumit Oct 18 '17 at 11:18
2
$\begingroup$

You could also use "TemperatureMap" is in-built color scheme,e.g.:

cf = ColorData["TemperatureMap", Rescale[#, MinMax[data[[All, 3]]]]] &;
Row[{Graphics[{cf@#3, PointSize[0.04], 
      Tooltip[Point[{#1, #2}], #3]} & @@@ data, Frame -> True, 
   ImageSize -> 200],
  BarLegend[{cf[#] &, {0, 100}}, Range[0, 100, 10]]}]

enter image description here

$\endgroup$
  • 1
    $\begingroup$ You can use that ColorFunction in ListPlot, too, as the OP seems to want: ListPlot[Style[{#[[1]], #[[2]]}, ColorData["TemperatureMap", Rescale[#[[3]], MinMax[data[[All, 3]]]]]] & /@ data, PlotStyle -> PointSize[Large]]. $\endgroup$ – aardvark2012 Oct 18 '17 at 10:56
  • $\begingroup$ @aardvark2012 very good point...put it is an answer as it uses ListPlot :) $\endgroup$ – ubpdqn Oct 18 '17 at 11:00
  • $\begingroup$ @aardvark2012: yes please ... I would like to see also the solutions with ListPlot. $\endgroup$ – mrz Oct 18 '17 at 11:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.