How to do a surface plot of a function

pgfWithContourLines Pgfplots can compute the z contours by means of gnuplot and its contour gnuplot interface. How to for Windows:

  • Install gnuplot with the option of adding gnuplot path to to the search PATH enter image description here

  • Reboot

  • Add "--enable-write18" to your LaTeX command. Here shown for pdflatex and TexStudio:enter image description here This is needed so gnuplot can be executed.
  • You should be able to genererate the graph using the following code:

    \documentclass[tikz]{standalone}
    \usepackage{pgfplots}
    \usepackage{amsmath}
    \begin{document}
    \begin{tikzpicture}
        \begin{axis}[
            declare function = {Z(\x,\y) = (\y+ln(1-\x))^2/\y^2;},
            domain=0:0.5,
            y domain=1:10,
            point meta max=1,
            point meta min=0,
            samples=40,
            colorbar,
            colormap/bluered,
            colorbar style={title=$z$},
            view={0}{90},
            xmin=0,
            xmax=0.5,
            xlabel={x},
            zmin=0,
            zmax=1,
            colorbar,
            ylabel={y}]
            \addplot3 [surf,shader=interp] {Z(x,y)};
            \addplot3[
                contour gnuplot={
                    levels={0.4,0.6,0.7,0.8,0.9,0.95},
                    contour label style={
                        nodes={text=black,opacity=0,text opacity=1,anchor=south},
                        %/pgf/number format/fixed,
                        /pgf/number format/fixed zerofill=true,
                        %/pgf/number format/precision=1
                        },                  
                    output point meta=rawz,
                    %number=10,
                    %labels=false,
                    draw color = black
                },
                samples=41,
                contour/draw color={black},
                contour/label distance=80pt
                ]
                {Z(x,y)};
        \end{axis}[]
    \end{tikzpicture}%
    \end{document}
    

I would be grateful for further improvements.


Try Asymptote inline mode:

% cont.tex:
%
\documentclass{article}
\usepackage[inline]{asymptote}
\usepackage{lmodern}
\begin{document}
\begin{figure}
\centering
\begin{asy}
import graph;
import palette;
import contour;

real w=6cm,h=w;
size(w,h,IgnoreAspect);
import fontsize;defaultpen(fontsize(7pt));
real f(real x, real y) {return (y+log(1-x))^2/y^2;}

pair a=(  0, 1);
pair b=(0.5,30);

int N=200;
int Divs=10;
int divs=2;

defaultpen(0.4bp);
pen Tickpen=black;
pen tickpen=gray+0.5*linewidth(currentpen);
pen[] Palette=Gradient(rgb("A42420"),rgb("FFF2B4"));

bounds range=image(f,Automatic,a,b,N,Palette);

// Major contours

real[] Cvals=uniform(range.min,range.max,Divs);
draw(contour(f,a,b,Cvals,N,operator --),Tickpen);
// Minor contours
real[] cvals;
for(int i=0; i < Cvals.length-1; ++i)
  cvals.append(uniform(Cvals[i],Cvals[i+1],divs)[1:divs]);
draw(contour(f,a,b,cvals,N,operator --),tickpen);

xaxis("$x$",BottomTop, RightTicks(Step=0.1,step=0.02),above=true);
yaxis("$y$",LeftRight,LeftTicks(Step=6,step=1),above=true);

palette("$f(x,y)=\displaystyle\frac{(y+\log(1-x))^2}{y^2}$",range,point(NW)+(0,0.5),point(NE)+(0,3),Top,Palette,
        PaletteTicks(N=Divs,n=divs,Tickpen,tickpen));
\end{asy}
\caption{A surface/contour plot of $(y+\ln(1-x))^2/y^2$}
\end{figure}
\end{document}
%
%   Process this file as:
%
% pdflatex cont.tex
% asy cont-*.asy    
% pdflatex cont.tex

enter image description here