# 4D Plot: ListPlot3D + Colorfunction

just in case someone has a similar problem: I wanted to visualize a 4D Listdata. The first 3 coordinates describe a surface and the fourth dimension a scalar value.

A classical example are Finite Element results for shells, where stress is plotted on a surface; or alternatively a temperature-field on a 3D surface.

I found some very helpful solutions [1,2,3,4], but none of them really solved my problem, because I wanted to have a continuous function and not a point cloud, and it couldn't get the last solution working in my case. I thought the solution might be worth sharing:

For me finally this thing here worked: Function f describes the surface; function g the colorfunction.

Creating datapoints for standalone example:

List4D = {};
f[{x_, y_}] := 1/2*(x^2 + y^2)
g[{x_, y_}] := x + y
xyList = Tuples[Range[-1, 1, 1/30], 2];
For[i = 1, i <= Length[xyList], i = i + 1,
List4D = Append[List4D,
Flatten[{xyList[[i]], f[xyList[[i]]], g[xyList[[i]]]}]];]


Plot surace + color

nf = Nearest[List4D[[All, {1, 2}]] -> Rescale[List4D[[All, 4]]]];
colfun = ColorData["Rainbow"]@First@nf[{#1, #2}] &;
ListPlot3D[List4D[[All, {1, 2, 3}]], ColorFunction -> colfun,
ColorFunctionScaling -> False]


Just in case you need a BarLegend:

BarLegend[{"Rainbow", {Min[List4D[[All, 4]]], Max[List4D[[All, 4]]]}}]