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


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];

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]

enter image description here

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.)

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

enter image description here

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

  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}.

  • $\begingroup$ 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? $\endgroup$ – user1541776 May 25 '17 at 2:53
  • 1
    $\begingroup$ @user1541776 Sorry, I forgot to address that, and it is simple and important. I shall update my answer. $\endgroup$ – Mr.Wizard May 25 '17 at 19:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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