I want to plot a scalar-field f(x,y)=sin(x2+y2)x2+y2f(x,y) = \frac{sin(x^2+y^2)}{x^2+y^2} and it’s gradient-field ∇f(x,y)\nabla f(x,y) in one graphic. Something like “StreamDensityPlot” in 3D. The mesh-lines on the scalar-field should look like arrows which are showing in the direction of the gradient.

Thanks very much 🙂

=================

related: Plot3d: How to color a surface according to the slope of the surface?

– Kuba

Feb 28 at 11:53

=================

2 Answers

2

=================

Expanding on rewi’s answer if you want the 3D result:

f[x_, y_] := Sin[x^2 + y^2]/(x^2 + y^2)

sdp =

StreamDensityPlot[ Evaluate[{Grad[f[x, y], {x, y}], f[x, y]}], {x, -3,3}, {y, -3, 3}, PlotRangePadding -> 0, Frame -> False]

Plot3D[f[x, y], {x, -3, 3}, {y, -3, 3}, PlotRange -> All, PlotStyle -> Texture[sdp], Mesh -> None]

Edit: Is there a bug with using StreamStyle with Texture?

sdp = StreamDensityPlot[

Evaluate[{Grad[f[x, y], {x, y}], f[x, y]}], {x, -3, 3}, {y, -3, 3},

PlotRangePadding -> 0, Frame -> False,

ColorFunction -> “SolarColors”,

StreamStyle -> White

]

The styling isn’t carried over:?

Plot3D[f[x, y], {x, -3, 3}, {y, -3, 3}, PlotRange -> All,

PlotStyle -> Texture[sdp], Mesh -> None]

But if we use StreamColorFunction:

sdp = StreamDensityPlot[

Evaluate[{Grad[f[x, y], {x, y}], f[x, y]}], {x, -3, 3}, {y, -3, 3},

PlotRangePadding -> 0, Frame -> False,

ColorFunction -> “SolarColors”,

StreamColorFunction -> (GrayLevel[1] &)

]

Plot3D[f[x, y], {x, -3, 3}, {y, -3, 3}, PlotRange -> All,

PlotStyle -> Texture[sdp], Mesh -> None]

Thank you very much 🙂 How can I change the colour of the scalarfield and the arrows ?

– AstroNerd

Feb 28 at 11:36

If you look up StreamDensityPlot you’ll find many options and examples of changing the various colors and stylings. You have to change them in the StreamDensityPlot, not the Plot3D as the former is used as a texture map for the later. I’ll add an example to my answer.

– Quantum_Oli

Feb 28 at 11:40

Is this what you are looking for?

f = Sin[x^2 + y^2]/(x^2 + y^2);

StreamDensityPlot[Evaluate[{Grad[f, {x, y}], f}], {x, -5, 5}, {y, -5, 5}]

I want exactly this in 3D 🙂

– AstroNerd

Feb 28 at 11:24