# Plot colored grid

Goal: plot 3D lattice with knots of different color.

Input:

xyzF={{1, 1, 1, color111}, {1, 1, 2, color112},...,{N,N,N,colorNNN}}


Working example to color the grid into Hue[x]:

xyzF = Flatten[Table[{i, j, k, i*j*k/27.}, {i, 1, 3}, {j, 1, 3}, {k, 1, 3}], 2]
xyz = Map[#[[1 ;; 3]] &, xyzF]
ListPointPlot3D[xyz, ColorFunction -> Function[{x, y, z}, Hue[x]]]


NOT working example to color the grid into F[x,y,z]:

returnF[x_, y_, z_] := Module[{res},

Map[If[#[[1]] == x && #[[2]] == y && #[[3]] == z, res = #[[4]]] &, xyzF];
Return[Hue[res]]

];
ListPointPlot3D[xyz, ColorFunction -> Function[{x, y, z}, returnF[x,y,z]]]


Error message:

Ignoring invalid graphics directive Hue[$CellContext$784].


What is the problem and how can I make it work?

### Source of the problem

Your code works as written with a single change: the addition of ColorFunctionScaling -> False. (You do not need a separate Function as returnF can be used independently.)

ListPointPlot3D[xyz, ColorFunction -> returnF, ColorFunctionScaling -> False]


ColorFunctionScaling defaults to True, therefore all coordinates are scaled to fall within the range [0,1] and these values do not match anything in xyzF.

### Recommended method

However you can simplify things significantly by using Graphics3D directly. (I shall use a larger PointSize so that you can see the coloration more easily.)

Graphics3D[{
PointSize[0.02],
{Hue[#4], Point[{#, #2, #3}]} & @@@ xyzF
}
, Axes -> True
]


Or a bit more efficiently (i.e. faster rendering) with a single Point expression and VertexColors:

Graphics3D[{
PointSize[0.02],
Point[xyz, VertexColors -> (Hue /@ xyzF[[All, 4]])]
},
Axes -> True
]


See BoxRatios if you want the graphic to have a different shape, e.g. ListPointPlot3D defaults to BoxRatios -> {1, 1, 0.4}`.

• Thank you, Mr.Wizard! It does the trick, but I'm a bit confused about the error message. Do you understand what's going on there? May 25, 2017 at 2:53
• @user1541776 Sorry, I forgot to address that, and it is simple and important. I shall update my answer. May 25, 2017 at 19:47