Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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 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 –  david sedai Jan 3 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 at 6:07
add comment

2 Answers

up vote 2 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
add comment

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
add comment

Your Answer

 
discard

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.