# What are the possible ways of visualizing a 4D function in Mathematica?

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)

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

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


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


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


Image3D[values, ColorFunction -> Hue]


Image3D[values]


• 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.
– amr
Commented Feb 14, 2013 at 2:59
• @amr yes, that will be other way. I added examples. Commented Feb 14, 2013 at 3:27
• 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. Commented Feb 18, 2013 at 14:47
• 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? Commented Mar 5, 2021 at 15:33
• @Kvothe something like this? Manipulate[ Image3D[{values[[i]]}, ColorFunction -> Hue], {i, 1, Length[values], 1}] Commented Mar 5, 2021 at 19:57

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"}]


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"}]


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


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]