Filling the space below a surface plot

Here I fill the space below the function z = x^2+y^2 to get a solid object. I did it by:

  • Plotting the curves at the edges, for x=0 and y=0
  • Plotting the bottom edges
  • Naming the paths above using name path
  • Using the fillbetween library to fill the space between top boundary curves and the bottom edges
  • Defining a pgf layer for the background and plotting the original surface z = x^2+y^2 on it, so the boundary areas on the main layer hide parts of the function
  • Shading the boundary areas improve the 3D look

My oringinal post on

Filling the space

  \pgfdeclarelayer{pre main}
  \pgfsetlayers{pre main,main}
      hide axis,
      domain = -4:4,
      zmax   = 12,
    \begin{pgfonlayer}{pre main}
      \addplot3 [surf] {(x^2+y^2)/4};
    \addplot3 [name path = xline, draw = none] (x,-4,0);
    \addplot3 [name path = yline, draw = none] (4,y,0);
    \addplot3 [name path = xcurve, y domain = 0:0, draw = none]
      (x, -4, {(x^2+16)/4});
    \addplot3 [name path = ycurve, y domain = 0:0, draw = none]
      (4, x, {(16+x^2)/4});
    \addplot [left color = black, right color = black!50, draw = none]
      fill between[of = xcurve and xline];
    \addplot [left color = black!50, right color = black, draw = none]
      fill between[of = yline and ycurve, reverse = true];
Click to download: fill-space-3d.texfill-space-3d.pdf