# Plot a function on a 2D surface

My Question

How do you plot a function $$f(x,y)=z$$ on a 2D surface $$g(x,y)=z$$.

A Simple Example

f[x_, y_] := (x - 0.5)^2 + (y - 0.5)^2;
g[x_, y_] := y Sin[x y]
DensityPlot[f[x, y], {x, 0, 2}, {y, 0, 2}]
(* 2D surface I want to plot on *)
Plot3D[g[x, y], {x, 0, 2}, {y, 0, 2}]


How can I plot the first on the second?

Notes

• ColorFunction? – ktm Mar 6 at 20:09
• Could you post a working example with ColorFunction. – AzJ Mar 6 at 20:11

# ColorFunction

Consider:

Plot3D[g[x, y], {x, 0, 2}, {y, 0, 2},
ColorFunction ->
Function[{x, y, z}, ColorData["RustTones"][f[x, y]/4.5]],
ColorFunctionScaling -> False] Replace "RustTones" with your favorite value from ColorData["Gradients"] (I do not know how to mimic the original DensityPlot color scheme).

# The DensityPlot output + PlotStyle

Alternatively, use the original DensityPlot as a texture:

img = DensityPlot[f[x, y], {x, 0, 2}, {y, 0, 2}, Frame -> False,
Plot3D[g[x, y], {x, 0, 2}, {y, 0, 2}, PlotStyle -> Texture[img]] • Thank you the second option is really useful. – AzJ Mar 6 at 20:29

You can also use a custom color function using your function f and the default color function for DensityPlot (which is "M10DefaultDensityGradient") as follows:

minmax = Through[{NMinValue, NMaxValue}[{f[x, y], 0 <= x <= 2 && 0 <= y <= 2}, {x, y}]];

cF1 = ColorData["M10DefaultDensityGradient"][Rescale[f[#, #2], minmax]] &;

Plot3D[g[x, y], {x, 0, 2}, {y, 0, 2}, ColorFunction -> cF1,
ColorFunctionScaling -> False, Lighting -> "Ambient", ViewPoint -> {1.5, -.5, 3}] Use a different gradient color scheme, say "CMYKColors", instead of "M10DefaultDensityGradient" to get: 