46
$\begingroup$

I have a function $F$ that maps the xyz space to a set of reals, more clearly:

$c = F[x,y,z]$

Where $c$,$x$,$y$ and $z$ are reals.

What are the possible ways of visualizing this 3d function in Mathematica? (if possible, please post a how-to-do-it)

$\endgroup$
4
  • $\begingroup$ I have something that could be interesting. I'll put it up tomorrow. $\endgroup$
    – rcollyer
    Commented Feb 14, 2013 at 3:09
  • $\begingroup$ @h3now you could check en.wikipedia.org/wiki/Parallel_coordinates to get some other ideas? $\endgroup$
    – Lou
    Commented Feb 14, 2013 at 8:29
  • $\begingroup$ Contour plot. You can plot the surfaces along which $F$ is constant. $\endgroup$
    – a06e
    Commented Aug 27, 2014 at 20:32
  • $\begingroup$ You can use - ListDensityPlot3D[] $\endgroup$
    – q than a
    Commented Aug 13, 2021 at 15:51

3 Answers 3

40
$\begingroup$

One possible way is to use Graphics3D with Point and color points by function value so it's like density plot 3d. For example,

xyz = Flatten[
         Table[{i, j, k}, {i, 1, 10, .35}, {j, 1, 10, .35}, {k, 1, 
          10, .35}], 2];

f[x_, y_, z_] := x^2 y Cos[z]

Graphics3D[
       Point[xyz, VertexColors -> (Hue /@ Rescale[f[##] & @@@ xyz])], 
 Axes -> True, AxesLabel -> {x, y, z}]

enter image description here

Another possible choice is just thinking one parameter as time variable and use Manipulate:

Manipulate[Plot3D[f[x, y, z], {x, 1, 10}, {y, 1, 10}], {z, 1, 10}]

enter image description here

There should be many other way to visualize 4d data, but it's really depending on what you want to see and how you want to visualize.

Like amr suggested, you can also use Image3D or Raster3D:

values = Rescale[
    Table[f[i, j, k], {i, 1, 10, .2}, {j, 1, 10, .2}, {k, 1, 10, .2}]];

Graphics3D[Raster3D[values, ColorFunction -> Hue]]

enter image description here

Image3D[values, ColorFunction -> Hue]

enter image description here

Image3D[values]

enter image description here

$\endgroup$
5
  • $\begingroup$ I was thinking something like this but with the Image3D functionality from Version 9. Not sure what the difference might be but it may look "smoother." I only have Mathematica 8 at the moment though so I can't test it. $\endgroup$
    – amr
    Commented Feb 14, 2013 at 2:59
  • 1
    $\begingroup$ @amr yes, that will be other way. I added examples. $\endgroup$
    – halmir
    Commented Feb 14, 2013 at 3:27
  • $\begingroup$ Thank you, great answer. Just so you know I needed this to visualize the error in a 3d position-estimate in the xyz space, so I'll most likely try something like your first example. $\endgroup$
    – JLagana
    Commented Feb 18, 2013 at 14:47
  • $\begingroup$ If I want to do something like Manipulate[Plot3D[f[x, y, z], {x, 1, 10}, {y, 1, 10}], {z, 1, 10}] for list data how would you go about that? $\endgroup$
    – Kvothe
    Commented Mar 5, 2021 at 15:33
  • $\begingroup$ @Kvothe something like this? Manipulate[ Image3D[{values[[i]]}, ColorFunction -> Hue], {i, 1, Length[values], 1}] $\endgroup$
    – halmir
    Commented Mar 5, 2021 at 19:57
31
$\begingroup$

A typical way of visualizing functions of the form $f(x,y,z)$ is in terms of level sets. One uses ContourPlot3D in Mathematica. Here I show it in conjunction with the function's gradient field, which may be omitted.

Manipulate[
 Show[
   ContourPlot3D[f == c, {x, -2, 2}, {y, -2, 2}, {z, -2, 2}, 
    ContourStyle -> Opacity[0.5]],
   ControlActive[{}, VectorPlot3D[Evaluate[D[f, {{x, y, z}}]], {x, -2, 2}, {y, -2, 2}, {z, -2, 2}]]
  ],
 {{f, x^2 + x y z + z^4}, InputField},
 {{c, 0.1}, -0.25, 5, Appearance -> "Labeled"}]

output

You mention in a comment visualizing error. I wasn't sure exactly what you were after, but you can plot contours plus or minus a given error in the value of f fairly easily.

Manipulate[
 Show[
  ContourPlot3D[f, {x, -2, 2}, {y, -2, 2}, {z, -2, 2}, 
   Contours -> c + {-dc, 0, dc}, 
   ContourStyle -> {Directive[Opacity[0.3], Red], Opacity[0.3], 
     Directive[Opacity[0.3], Blue]}, Mesh -> None],
  ControlActive[{}, VectorPlot3D[Evaluate[D[f, {{x, y, z}}]], {x, -2, 2}, {y, -2, 2}, {z, -2, 2}]]
  ],
 {{f, x^2 + x y z + z^4}, InputField},
 {{c, 0.6}, -0.25, 5, Appearance -> "Labeled"},
 {{dc, 0.5}, 0, 1., Appearance -> "Labeled"}]

output

$\endgroup$
23
$\begingroup$

As of version 10.2 one can use DensityPlot3D:

DensityPlot3D[x^2 y Cos[z], {x, 1, 10}, {y, 1, 10}, {z, 1, 10}, 
  ColorFunction -> "Rainbow", AxesLabel -> {x, y, z}]

enter image description here

The transparent regions can be set manually and will be indicated on the left side of the bar legend:

of[f_] := Max[.02, Abs[2f - 1]^1.5]

DensityPlot3D[x^2 y Cos[z], {x, 1, 10}, {y, 1, 10}, {z, 1, 10}, 
  ColorFunction -> "Rainbow", AxesLabel -> {x, y, z},
  OpacityFunction -> of, PlotLegends -> Automatic]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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