5
$\begingroup$

Suppose a data array of the form {{x1,y1,z1},{x2,y2,z2}...}, is it possible to use ListPlot where x,y specify the point position and z (Hue[z]) specify the color of each point?

$\endgroup$
3
  • $\begingroup$ yes, use the ColorFunction see ref/ListPlot3D under Color by scaled x, y, and z values. there is example there to do this. $\endgroup$
    – Nasser
    Jan 3, 2014 at 5:48
  • $\begingroup$ @Nasser Yes, I know that can be done with 3D list plots, but I want a 2D plot where z is only used for coloring $\endgroup$ Jan 3, 2014 at 5:58
  • $\begingroup$ So you only want to plot the {x,y} part of the list? and use the z component for coloring of the point? I miss-understood. $\endgroup$
    – Nasser
    Jan 3, 2014 at 6:07

2 Answers 2

7
$\begingroup$

If you really must use ListPlot and not Graphics as Ubpdqn showed, then here is a hack.

The problem is that ColorFunction for 2D takes only {x,y}. One way is to get the z value yourself from the data. But for this you need the current index.

So simply make an index, set it to zero, and increment it each time the ColorFunction is called. Like this. Notice, must use Joined -> True for this to work.

data = RandomReal[{0, 1}, {40, 3}];
n = 0; (*make sure to set this to zero before each call*)
ListPlot[data[[All, {1, 2}]],PlotStyle->Directive[PointSize[Large]],Joined -> True,
  ColorFunction -> Function[{x, y}, Hue[data[[++n, 3]]] ]] /. Line[a__] :> Point[a]

Mathematica graphics

reference 2D ListPlot with separate color function

$\endgroup$
1
  • $\begingroup$ Thank you for the solution. How could I use a certain color map like "TemperatureMap or my own color table: number = Length@data[[All,3]]; colTable = {Table[{Blend[{Blue, Green, Yellow, Red}, x]}, {x, 1/number, 1, 1/number}]}; colTable = Flatten[colTable]; $\endgroup$
    – mrz
    Oct 18, 2017 at 9:01
11
$\begingroup$

You could achieve this using Graphics, for example:

data = RandomReal[{0, 1}, {100, 3}];
Graphics[{Hue[#3], PointSize[0.02], Point[{#1, #2}]} & @@@ data, 
 Axes -> True]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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