11
$\begingroup$

I know it is possible to create a filling in between two curves on a 2D plot, but is it possible to do the same in 3D?

My attempt like this:

Z1 := E^(-x^2) Cos[x^2 + y^2]
Z2 := 2 - x^2 - y^2

Plot3D[{Z1, Z2}, {x, -1, 1}, {y, -1, 1}, AspectRatio -> 1, 
ViewPoint -> {4, 1, 1}, Filling -> Top, FillingStyle -> Opacity[0.9]]

yields the graph below:

enter image description here

Is there a way I can fill the graph between the two aforementioned 3D surfaces?

Thank you for your time.

$\endgroup$
11
  • $\begingroup$ @IstvánZachar Thanks, it's fixed now... $\endgroup$ Nov 20 '12 at 16:57
  • $\begingroup$ Yes, I do remember now: this is an old bug I've encountered earlier. Simpler version: Plot3D[{1, 2}, {x, 0, 1}, {y, 0, 1}, Filling -> {1 -> {2}}] Sadly, I don't know any workaround. $\endgroup$ Nov 20 '12 at 17:07
  • $\begingroup$ @IstvánZachar Just wondering... regarding the Plot3D[{1, 2}... what is the {1, 2} plotting, just 1 and 2 in three space? $\endgroup$ Nov 20 '12 at 17:11
  • $\begingroup$ @IstvánZachar Your solution usually works for 2D plots, but doesn't seem to work for 3D. $\endgroup$
    – VLC
    Nov 20 '12 at 17:13
  • 3
    $\begingroup$ should this be listed as a bug (given the comments and answers?) $\endgroup$ Nov 20 '12 at 20:46
11
$\begingroup$

How do you like this?

RegionPlot3D[
 2 - x^2 - y^2 > z > E^(-x^2) Cos[x^2 + y^2], {x, -1, 1}, {y, -1, 
  1}, {z, -0.2, 2}, Mesh -> False, AspectRatio -> 1, 
 ViewPoint -> {4, 1, 1}, PlotStyle -> {LightBlue, Opacity[0.8]}]

Mathematica graphics

$\endgroup$
1
  • $\begingroup$ I like it! +1 for simplicity! $\endgroup$ Nov 21 '12 at 13:23
7
$\begingroup$

As far as I understand the documentation, the following should work:

f[x_, y_] = E^(-x^2) Cos[x^2 + y^2];
g[x_, y_] = 2 - x^2 - y^2;
pic = Plot3D[{f[x, y], g[x, y]}, {x, -1, 1}, {y, -1, 1},
  AspectRatio -> 1, ViewPoint -> {4, 1, 1},
  Filling -> {1 -> {2}}, FillingStyle -> Opacity[0.9]]

enter image description here

The fact that it doesn't looks like a bug to me. The following might be a reasonable workaround:

{{a, b}, {c, d}} = {{-1, 1}, {-1, 1}};
dx = (b - a)/80;
dy = (d - c)/80;
fillEdges = Graphics3D[{Opacity[0.9], EdgeForm[],
    Polygon[Join[
      Table[{
        {x, c, f[x, c]}, {x + dx, c, f[x + dx, c]},
        {x + dx, c, g[x + dx, c]}, {x, c, g[x, c]}},
       {x, a, b - dx, dx}],
      Table[Reverse@{
         {x, d, f[x, d]}, {x + dx, d, f[x + dx, d]},
         {x + dx, d, g[x + dx, d]}, {x, d, g[x, d]}},
       {x, a, b - dx, dx}],
      Table[{
        {a, y, f[a, y]}, {a, y + dy, f[a, y + dy]},
        {a, y + dy, g[a, y + dy]}, {a, y, g[a, y]}},
       {y, c, d - dy, dy}],
      Table[{
        {b, y, f[b, y]}, {b, y + dy, f[b, y + dy]},
        {b, y + dy, g[b, y + dy]}, {b, y, g[b, y]}},
       {y, c, d - dy, dy}]
      ]]}];
Show[{pic, fillEdges}]

enter image description here

$\endgroup$
1
  • $\begingroup$ I tried the same thing. Any ideas on why the original solution that follows the documentation does not work? $\endgroup$ Aug 14 '18 at 1:18
6
$\begingroup$

This is a rough solution that does what you're looking for:

Z1[x_, y_] := E^(-x^2) Cos[x^2 + y^2]
Z2[x_, y_] := 2 - x^2 - y^2

side1 = Join[Table[{-1, y, Z1[-1, y]}, {y, -1, 1, .1}], 
   Reverse[Table[{-1, y, Z2[-1, y]}, {y, -1, 1, .1}]]];
side2 = Join[Table[{1, y, Z1[1, y]}, {y, -1, 1, .1}], 
   Reverse[Table[{1, y, Z2[1, y]}, {y, -1, 1, .1}]]];
side3 = Join[Table[{x, -1, Z1[x, -1]}, {x, -1, 1, .1}], 
   Reverse[Table[{x, -1, Z2[x, -1]}, {x, -1, 1, .1}]]];
side4 = Join[Table[{x, 1, Z1[x, 1]}, {x, -1, 1, .1}], 
   Reverse[Table[{x, 1, Z2[x, 1]}, {x, -1, 1, .1}]]];

Show[
 Plot3D[{Z1[x, y], Z2[x, y]}, {x, -1, 1}, {y, -1, 1}, 
  AspectRatio -> 1, ViewPoint -> {4, 1, 1}],
 Graphics3D[{Opacity[.9], Polygon[side1], Polygon[side2], Polygon[side3], 
   Polygon[side4]}]
 ]

enter image description here

$\endgroup$
2
$\begingroup$

I would suggest using ParametricPlot3D to draw the "filling" polygons:

Z1[x_, y_] := E^(-x^2) Cos[x^2 + y^2]
Z2[x_, y_] := 2 - x^2 - y^2

Show[Plot3D[{Z1[x, y], Z2[x, y]}, {x, -1, 1}, {y, -1, 1}],
 ParametricPlot3D[{
   {x, -1, t Z1[x, -1] + (1 - t) Z2[x, -1]},
   {x, 1, t Z1[x, 1] + (1 - t) Z2[x, 1]},
   {-1, x, t Z1[-1, x] + (1 - t) Z2[-1, x]},
   {1, x, t Z1[-1, x] + (1 - t) Z2[1, x]}}
  , {t, 0, 1}, {x, -1, 1}],
 BoxRatios -> Automatic]

enter image description here

$\endgroup$

This site is temporarily in read only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .