# How to make a 2D scatter plot colored by the value of a third parameter

As the title suggests I'm trying to make a 2D (x-y) scatter plot of the list {x,y,z}, colored by the value of a third parameter (z). I came the closest by using ListPointPlot3D, but unfortunately that gives a 3D box plot whereas I want to get something like this

without the contour lines. Any help would be greatly appreciated.

P.S. Any ideas for a legend regarding the "colors - z values" would be very helpful as well.

• Does ListPlot[Style[{#1, #2}, Hue[#3]] & @@@ data] work for you? it's a bit hard to tell without code (or without knowing how you want to convert the z value into color) Jun 19, 2021 at 1:09
• Please post your code.Maybe DensityPlot Jun 19, 2021 at 1:34

## 2 Answers

As far as I am aware, there is no dedicated built-in function for this task, which I find rather strange given how often this is needed. (Unless something that I am not aware of was added recently.) Therefore, I usually implement this from scratch.

Let us generate some example data:

data = RandomReal[{-1, 1}, {20, 3}];


There actually is a similar function, but it is designed for small datasets: BubbleChart. We can turn off the bubble sizing to get what you want:

BubbleChart[data, BubbleSizes -> .1 {1, 1},
ColorFunction -> "Rainbow"]


If you have large data, you can do something like this:

(* Choose a colour function: *)
cf = ColorData["Rainbow"]

Graphics[{
PointSize[0.03],
Point[data[[All, 1 ;; 2]], VertexColors -> cf /@ Rescale[data[[All, 3]]]]
}]


This is a very fast rendering solution because I used a single Point primitive instead of separate ones for each point. It takes maybe 1-2 seconds for ~100,000 points, and it can handle a million if you are patient and Rasterize the output (although at that point it makes sense to start histogramming the data).

If for some reason I need separate styling for each point (e.g. to control sizes as well) or I need to use Disk[] instead of Point[] to prioritize quality over performance, I do something like the following:

Graphics[
MapThread[
{cf[#2], Disk[#1, 0.03]} &,
{data[[All, 1 ;; 2]], Rescale[data[[All, 3]]]}]
]


As for the legend, you also need to do it manually. For example:

mima = MinMax[data[[All, 3]]];
BarLegend[{ColorData[{"Rainbow", mima}], mima}]

• This works excellent and fast, do you know if it's possible to add the x-y axis as well, to make it look like a complete plot? Jun 19, 2021 at 13:17
• @CoolCookie Look up the options available for Graphics. You can use Axes -> True or Frame -> True, whichever you prefer. There are many other options to customize this. Jun 19, 2021 at 13:18
• Thank you very much!! Jun 19, 2021 at 13:55

In versions 12.2+, you can use PointValuePlot which allows multiple parameters that can be associated with color/size/shape/... of 2D points:

SeedRandom[1];
data = RandomReal[{0, 1}, {8, 6}];

PointValuePlot[data[[All, ;; 2]] ->
MapAt[ToString[Round[#, .1]] &, data[[All, 3 ;;]], {All, {1, -1}}],
{2 -> "Color", 3 -> "Size", 4 -> "Shape"},
ColorFunction -> "Rainbow",
PlotLegends -> Automatic,
PlotRange -> All]