I have seen some similar posts but could not quite find what I am looking for.

I would like to create a 2D plot with discrete points that are color coded by a 3rd dimension value. The color function I am trying for is a rainbow color scheme that changes linearly between the smallest and largest values of the 3rd dimension. This would be similar to what ListPointPlot3D seems to do automatically if you specify a color function, but with a 2D plot. Here is the example of the output of the ListPointPlot3D color coded based on the z dimension:

example 3D with correct color function coloring

From the code

ListPointPlot3D[data, ColorFunction -> "Rainbow",PlotRangePadding -> None, BaseStyle -> {14, Bold}]

The example data can be found here: https://www.dropbox.com/s/6mvpo1kiw5b1t2o/example_points_color_coding.csv?dl=0

Any thoughts are greatly appreciated!

This example is not for geographic data, but it seems like this kind of function would also be very useful for geographic data, so I am surprised there is no built-in version for points on a 2D plot in Mathematica (although I could have missed it!).


1 Answer 1

lpp3d = ListPointPlot3D[data, ColorFunction -> "Rainbow"]

enter image description here

Rescale the last column of data, then style each pair of first two coordinates with ColorData["Rainbow"] evaluated at the rescaled value of the third column.

data2 = data;
data2[[All, 3]] = Rescale[data2[[All, 3]]];
ListPlot[Style[#[[;; 2]], ColorData["Rainbow"][#[[3]]]] & /@ data2, 
 AspectRatio -> 1, Frame -> True]

enter image description here

Alternatively, if the surface does not bend on itself, post-process the output of ListPointPlot3D to remove the third coordinates of Point primitives:

Graphics[Replace[lpp3d[[1]], Point[x_] :> Point[x[[;; 2]]], {0, Infinity}], 
   AspectRatio -> 1, Frame -> True]

enter image description here

  • 1
    $\begingroup$ Great! Thanks so much! The first solution works well for me. It doesn't appear to work with ListLogLogPlot unfortunately, but it works to plot the log of the data and re-label the axes. :) $\endgroup$ Jul 22, 2019 at 18:14
  • $\begingroup$ oh, that is slick! $\endgroup$
    – alex
    Apr 25 at 21:12

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.