5
$\begingroup$

The following function generates a plot of the 3d function indicated in the example.

Plot3D[(x^2 + y^2) Exp[1 - x^2 - y^2], {x, -3, 3}, {y, -3, 3}, 
 Mesh -> None, ImageSize -> Large, PlotPoints -> 35, 
 PlotStyle -> {Texture[
    StreamPlot[
     Evaluate[-D[(x^2 + y^2) Exp[1 - x^2 - y^2], {{x, y}}]], {x, -3, 
      3}, {y, -3, 3}, Frame -> None, ImageSize -> Large, 
     StreamStyle -> Black]]}]

However, when I choose a different ColorFunction parameter the texture (that only consists of arrows) disappears. Any idea how to correct this? I tried to make the background transparent, combine two 3D plots etc without success. Also, I have no idea why this is happening.

Here is the 3D plot without the gradient field.

Plot3D[(x^2 + y^2) Exp[1 - x^2 - y^2], {x, -3, 3}, {y, -3, 3}, 
 Mesh -> None, ImageSize -> Large, PlotPoints -> 35, 
 PlotStyle -> {Texture[
    StreamPlot[
     Evaluate[-D[(x^2 + y^2) Exp[1 - x^2 - y^2], {{x, y}}]], {x, -3, 
      3}, {y, -3, 3}, Frame -> None, ImageSize -> Large, 
     StreamStyle -> Black]]}, ColorFunction -> "Rainbow"]
$\endgroup$

3 Answers 3

9
$\begingroup$

The color is not quite right but the idea seems to work. Edit: much closer now.

dp = DensityPlot[(x^2 + y^2) Exp[1 - x^2 - y^2], {x, -3, 3}, {y, -3, 3}, 
   ColorFunction -> "Rainbow", PlotPoints -> 100];

sp = StreamPlot[
   Evaluate[-D[(x^2 + y^2) Exp[1 - x^2 - y^2], {{x, y}}]], {x, -3, 3}, {y, -3, 3}, 
   Frame -> None, ImageSize -> Large, StreamStyle -> Black];

tex = Show[dp, sp, Frame -> None, PlotRangePadding -> 0, ImageSize -> 500];

Plot3D[(x^2 + y^2) Exp[1 - x^2 - y^2], {x, -3, 3}, {y, -3, 3}, Mesh -> None, 
 ImageSize -> Large, PlotPoints -> 35
 , PlotStyle -> {Texture[Lighter[tex, 0.15]]}
 , Lighting -> "Neutral"
]

enter image description here

$\endgroup$
8
$\begingroup$

You can use StreamDensityPlot (which accepts the ColorFunction option) to produce the texture:

sdp = StreamDensityPlot[Evaluate[{-D[(x^2 + y^2) Exp[1 - x^2 - y^2], {{x, y}}], 
    (x^2 + y^2) Exp[1 - x^2 - y^2]}], {x, -3, 3}, {y, -3, 3}, 
   StreamStyle -> Black, 
   ColorFunction -> "Rainbow", 
   ColorFunctionScaling -> False, Frame -> False, Axes -> False, 
   PlotRangePadding -> None];
Plot3D[(x^2 + y^2) Exp[1 - x^2 - y^2], {x, -3, 3}, {y, -3, 3}, 
 Mesh -> None, ImageSize -> Large, PlotPoints -> 35, 
 PlotStyle -> Texture[Lighter@sdp], Lighting -> "Neutral"]

enter image description here

$\endgroup$
7
  • $\begingroup$ Slightly shorter: sdp = StreamDensityPlot[ Evaluate[{-D[#, {{x, y}}], #} &[(x^2 + y^2) Exp[ 1 - x^2 - y^2]]], {x, -3, 3}, {y, -3, 3}, StreamStyle -> Black, ColorFunction -> "Rainbow", Frame -> False, Axes -> False, PlotRangePadding -> None]; $\endgroup$
    – Michael E2
    Commented Jan 14, 2019 at 21:55
  • $\begingroup$ @MichaelE2, I tried that version; but the colors do not match the colors in Plot3D. $\endgroup$
    – kglr
    Commented Jan 14, 2019 at 23:01
  • $\begingroup$ Odd, they match your code above, for me. I switched between the two images and saw no (perceptible) difference. $\endgroup$
    – Michael E2
    Commented Jan 14, 2019 at 23:02
  • $\begingroup$ @MichaelE2, maybe version/os difference (i am using v 11.3 windows 10/64bit). $\endgroup$
    – kglr
    Commented Jan 14, 2019 at 23:04
  • $\begingroup$ @MichaelE2, ColorFunction -> "Rainbow" does work if the first argument of StreamDensityPlot has the form $\{\{v_x, v_y\}, s \}$. $\endgroup$
    – kglr
    Commented Jan 15, 2019 at 1:39
2
$\begingroup$

PlotStyle -> Texture[...] relies on VertexTextureCoordinates to map the texture to polygon vertices.

ColorFunction -> colorfunction relies on VertexColors to associate colors with the polygon vertices.

Only one of them actually gets to style the polygon. In my case, it seems to be the texture:

Graphics3D[{Texture[RandomImage[1, 100]], 
  Polygon[{{0, 0, 0}, {1, 0, 0}, {1, 1, 0}}, 
   VertexColors -> {Red, Green, Blue}, 
   VertexTextureCoordinates -> {{0, 0}, {1, 0}, {1, 1}}]}, 
 Lighting -> "Neutral", BoxRatios -> {1, 1, 1}]

enter image description here

It sounds like the color function is winning in your case. It wouldn't surprise me if that was dependent on things like OS, software version, phase of the moon, etc...

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.