I have a dataset in which each element is a list of 3 numbers eg.


I'd like to make a 2 dimensional plot in this way: for each list in the dataset the first 2 elements specify the point coordinate in the 2D plot and the third element specifies a color taken from a color gradient (for example using ColorData[]), so that close numbers have close colors; moreover, I'd like to show a bar legend with colors.

An example that solve the problem is this


Apart that maybe there is a bug in the frontend, since it says that «ColorFunctionScaling is not an option for BarLegend», can I obtain such a plot without using the Graphics command and related primitives?


1 Answer 1

data = {{0, 0, 0}, {1, 2, 0.7}, {3, 4, 2.3}, {3, 4, 3.3}};

mM = MinMax@data[[All, 3]];

cf = Blend[{Red, Blue}, Rescale[#, mM]] &;

dataPoints = Tooltip[{cf[#[[3]]], Point[Most@#]}, #[[3]]] & /@ data;

Legended[Graphics[{AbsolutePointSize[10], dataPoints}, Frame -> True],
  BarLegend[{cf, mM}]]

enter image description here

Note that two of your points are at the same coordinates {3, 4} and only the last will show. You could change your color function to cf = Blend[{Opacity[0.5, Red], Opacity[0.5, Blue]}, Rescale[#, mM]] &; then overlapped points will be darker than singular points. However, in general the colors would then be washed out.

EDIT: Using ListPlot

dataPoints = Tooltip[Style[Most@#, cf[#[[3]]]], #[[3]]] & /@ data;

  PlotStyle -> AbsolutePointSize[10],
  Frame -> True],
 BarLegend[{cf, mM}]]

enter image description here

  • $\begingroup$ Well, you've improved my answer but I was looking for an approach different from what I've done. For example, a specific Mathematica function would be a perfect solution. Note that other languages provide such functions. I'll modify my question to be more specific. Thanks anyway :) $\endgroup$
    – Giancarlo
    Nov 4, 2018 at 14:45

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.