6
$\begingroup$

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

enter image description here enter image description here

How can I plot the first on the second?

Notes

  • If you have any questions or need clarification please ask.
$\endgroup$
  • 1
    $\begingroup$ ColorFunction? $\endgroup$ – ktm Mar 6 at 20:09
  • $\begingroup$ Could you post a working example with ColorFunction. $\endgroup$ – AzJ Mar 6 at 20:11
10
$\begingroup$

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]

enter image description here

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, 
  PlotRangePadding -> None]
Plot3D[g[x, y], {x, 0, 2}, {y, 0, 2}, PlotStyle -> Texture[img]]

enter image description here

| improve this answer | |
$\endgroup$
  • 2
    $\begingroup$ Thank you the second option is really useful. $\endgroup$ – AzJ Mar 6 at 20:29
6
$\begingroup$

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

enter image description here

Use a different gradient color scheme, say "CMYKColors", instead of "M10DefaultDensityGradient" to get:

enter image description here

| improve this answer | |
$\endgroup$

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.