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?
2 Answers
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]
reference 2D ListPlot with separate color function
-
$\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$– mrzOct 18, 2017 at 9:01
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]
ColorFunction
see ref/ListPlot3D under Color by scaled x, y, and z values. there is example there to do this. $\endgroup$