PGFPlots – Modifying 3D Surface in PGFPlots

3dpgfplotstikz-pgf

I'm trying to modify a surface in pgfplots to get something similar to a Matlab result. The image below is what I want to achieve.

Matlab result

The code I used:

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{colormap/viridis,width=10cm,compat=1.9}

\begin{document}


\begin{tikzpicture}
\begin{axis}[
    colormap/viridis,
    view={60}{30},
]
\addplot3[
    surf,
    samples=25,
    domain=-3:3,
]
{x^2+4*y^2};
\addlegendentry{\(x^2+4y^2\)}
\end{axis}
\end{tikzpicture}

\end{document}

Result from the code

So basically what I'm trying to do is to remove the bottom of the pgfplots surface. The ellipse is defined by -2<x<2 and -1<y<1, with center in (0,0).

Best Answer

\documentclass[border=1 cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis}[colormap/viridis, view={40}{50}]
\addplot3[surf, shader=faceted interp, samples=25, domain=-3:3] {x^2+4*y^2};
\addlegendentry{\(x^2+4y^2\)}
\clip[preaction={fill=white}] plot[domain=-180:180, samples=100, variable=\t] ({2*cos(\t)},{sin(\t)},4);  
\addplot3[surf, shader=faceted interp, samples=25, domain=-3:3, unbounded coords=jump] {x^2+4*y^2>4 ? x^2+4*y^2 : nan};
\end{axis}
\end{tikzpicture}
\end{document}

Surface graph with elliptic cutout

or with shader=interp

Graph with smooth surface and no grid