Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
yes, use the ColorFunction see ref/ListPlot3D under Color by scaled x, y, and z values. there is example there to do this. – Nasser Jan 3 '14 at 5:48
@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 – egwene sedai Jan 3 '14 at 5:58
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. – Nasser Jan 3 '14 at 6:07
up vote 3 down vote accepted

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

share|improve this answer

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

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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