[Tex/LaTex] How to do a surface plot of a function

asymptotepgfplots

I would like to create a surface/contour plot of (y+ln(1-x))^2/y^2 for the range of x=0:0.5 and y=1:30.

Here the wanted result from Wolfram Alpha.

WolframAlphaOutput

However my attempt in PGF fails miserably:

    \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;},
            point meta max=1,
            point meta min=0,
            samples=40,
            view={0}{90},
            xmin=0,
            xmax=0.5,
            xlabel={x},
            ymin=1,
            ymax=30,
            zmin=0,
            zmax=1,
            colorbar,
            ylabel={y}]
            \addplot3 [surf] {Z(x,y)};
        \end{axis}[]
    \end{tikzpicture}%
    \end{document}

enter image description here

I would be grateful for help.

Best Answer

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.