2
$\begingroup$

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

enter image description here

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.

$\endgroup$
2
  • 3
    $\begingroup$ 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) $\endgroup$
    – thorimur
    Jun 19, 2021 at 1:09
  • 2
    $\begingroup$ Please post your code.Maybe DensityPlot $\endgroup$
    – cvgmt
    Jun 19, 2021 at 1:34

2 Answers 2

4
$\begingroup$

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"]

enter image description here

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}]
$\endgroup$
3
  • $\begingroup$ 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? $\endgroup$ Jun 19, 2021 at 13:17
  • $\begingroup$ @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. $\endgroup$
    – Szabolcs
    Jun 19, 2021 at 13:18
  • $\begingroup$ Thank you very much!! $\endgroup$ Jun 19, 2021 at 13:55
2
$\begingroup$

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] 

enter image description here

$\endgroup$

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.