pgfplots add tag
Anonymous 1123
I am rarely to use `pgfplots` to plot a surface, and then, I do not know How start to draw this graph. How to draw this graph?
![image.png](/image?hash=01342df68363d310582bf3610c353a7a985e68b3e6986f0a5da03865cfb7bd4f)
Top Answer
marmot
In general, `pgfplots` is a great tool for 3d plots. This is certainly the case if you plot a single surface, as in this example. If you have multiple intersecting plots, then you may have to work harder (but according to how I read the manual, the future may bring some tools that do such things automatically). To produces this plot, all we need to do is to use polar coordinates and a simple trick for the vertical wall: cut of the radius function via `rr(\x)=min(\x,1);`. Notice also that the axes are autmomatically almost right, but this is because they happen to be so in this example, so we only have to fix a stretch of the z-axis. In general, pgfplots does not compute the intersections of the axes with the surfaces, so we do have to do that ourselves. Note also that this plot does not really highlight some of the very nice features of `pgfplots` such as colorful shading with interpolated colors. 
```
\documentclass[tikz,border=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\begin{document}
\begin{tikzpicture}[line cap=round,line join=round,miter limit=5]
 \begin{axis}[width=15cm,unit vector ratio=1 1 1,
 	view={120}{30},axis lines=center,
    xtick=\empty,ytick=\empty,ztick=\empty,
	xlabel={$x$},ylabel={$y$},zlabel={$z$},
 	xmin=-2,ymin=-2,zmin=0,
    xmax=2,ymax=2,zmax=3,
 	declare function={f(\x,\y)=(\x<=1?2-\x*\x*(pow(cos(\y),2)/2+pow(sin(\y),2)):0);
		rr(\x)=min(\x,1);}]
  \addplot3[surf,fill=white,
  colormap/blackwhite,point meta=0,
  z buffer=sort,
  samples y=36,
  domain=0:1.04,y domain=0:360]
   ({rr(x)*cos(y)},{rr(x)*sin(y)},{f(x,y)}); 
  \draw (0,0,2) -- (0,0,2.4); 
  \path (0,0.5,2.5) node[right]{$\displaystyle z=2-\frac{x^2}{2}-y^2$};
 \end{axis}
\end{tikzpicture}
\end{document}
```
![Screen Shot 2021-02-03 at 5.26.20 PM.png](/image?hash=1ba0d0188b1fc06f366f0dcbb11ef098a656e2ef3f339e2cde67eb4cdfde7f9d)

Enter question or answer id or url (and optionally further answer ids/urls from the same question) from

Separate each id/url with a space. No need to list your own answers; they will be imported automatically.