10

I'm trying to plot a function using pgfplots:

\documentclass{scrartcl}

\usepackage{pgfplots}
\pgfplotsset{compat = 1.12}

\newcommand{\param}{2.0}

\begin{document}
        \begin{tikzpicture}
          \begin{axis}[view={40}{50}]
            \addplot3[surf, domain = 0:1, y domain = 0:1, unbounded coords=jump,
            samples = 50]
            {x^(-\param-1)*y^(-\param-1)*(x^(-\param)+y^(-\param)-1)^(-1/\param-2)+\param*x^(-\param-1)*y^(-\param-1)*(x^(-\param)+y^(-\param)-1)^(-1/\param-2)};
        \end{axis}
      \end{tikzpicture}
\end{document}

This results in:

Pgfplots output

Compared with (plotted in the OS X Grapher application)

OS X grapher output

the plot generated by pgfplots is a lot "rougher" near x=y=0. I have tried increasing the number of samples from the default to 50 but that hasn't really improved the plot much.

  • you need to increase both axis sample number such as samples = 50, samples y= 50 – percusse May 8 '15 at 15:27
  • I've just tried samples = 70, samples y = 70 - it doesn't seem to have any visible effect. – user2249626 May 8 '15 at 16:21
  • 1
    Your last term is power of a power or typo? – percusse May 10 '15 at 16:56
  • Use Gnuplot. I find that it handles surface plots better than pgfplots. – Holene May 10 '15 at 17:21
  • @percusse The last term is not a power of a power. What makes you think it might be (I can't spot any typo)? – user2249626 May 10 '15 at 17:50
9
+400

The default surface plot of pgfplots uses two triangles for each rectangular patch segment. Usually, the diagonal does not matter much -- but in this case, it really matters and the result is unsuitable.

Note that shader=interp appears to select the other diagonal (unintentionally, but it does). A simple solution would be to add shader=interp, unless you really need the grid lines.

\documentclass{standalone}

\usepackage{pgfplots}
\usepgfplotslibrary{patchplots}
\pgfplotsset{compat = 1.12}

\newcommand{\param}{2.0}

\begin{document}
        \begin{tikzpicture}
          \begin{axis}[view={40}{50}]
            \addplot3[surf, domain = 0:1, y domain = 0:1, unbounded coords=jump,
                shader=interp,
                samples = 25]
            {x^(-\param-1)*y^(-\param-1)*(x^(-\param)+y^(-\param)-1)^(-1/\param-2)+\param*x^(-\param-1)*y^(-\param-1)*(x^(-\param)+y^(-\param)-1)^(-1/\param-2)};
        \end{axis}
      \end{tikzpicture}
\end{document}

enter image description here

2

This answer is now an appendix (how to add gridlines) to C.F.'s (better) answer above: shader=interp is the surf equivalent of smooth, and it preserves the color scheme, unlike my original answer.

shader=interp removes gridlines, but you add some of them back in, because of the above fact, thus:

C.F.'s answer plus gridlines

\documentclass{article}\usepackage{pgfplots}\usepgfplotslibrary{patchplots}
\newcommand{\pt}{2}
\begin{document}\begin{tikzpicture}
    \begin{axis}[3d box,width=8cm,view={147}{56},
    domain=0:0.4,y domain=0:0.4,samples=32,
    xlabel=$x$,ylabel=$y$,zlabel={$z$},]
    \addplot3[surf,domain=0:0.4,y domain=0:0.4,unbounded coords=jump,shader=interp]
{%
x^(-\pt-1)*y^(-\pt-1)*(x^(-\pt)+y^(-\pt)-1)^(-1/\pt-2)+\pt*x^(-\pt-1)*y^(-\pt-1)*(x^(-\pt)+y^(-\pt)-1)^(-1/\pt-2)%
};
    \addplot3[domain=0:0.4,y domain=0:0.4,unbounded coords=jump,smooth]
{%
x^(-\pt-1)*y^(-\pt-1)*(x^(-\pt)+y^(-\pt)-1)^(-1/\pt-2)+\pt*x^(-\pt-1)*y^(-\pt-1)*(x^(-\pt)+y^(-\pt)-1)^(-1/\pt-2)%
};
\end{axis}\end{tikzpicture}\end{document}

The gridlines themselves are:

smooth no surf

Smoothing via cubic bézier curves is implemented in pgfplots. (See p.76 of the pgfplots manual.)

\addplot3[...,smooth,...] for line only or \addplot+[...,smooth,...] gives the above and fill it with blue points.

Consider

\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}
    \begin{tikzpicture}
      \begin{axis}[view={40}{50}]
        \addplot3 [y domain = 0:2,smooth]{-0.7+4*exp(-0.5*(x+3))*(3*cos(4*x*180/pi)+2.5*cos(2*x*180/pi))+0.5*y*y*4};
    \end{axis}
  \end{tikzpicture}
\end{document}

gliding

and compare to unsmoothed version

\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}
    \begin{tikzpicture}
      \begin{axis}[view={40}{50}]
        \addplot3 [y domain = 0:2]{-0.7+4*exp(-0.5*(x+3))*(3*cos(4*x*180/pi)+2.5*cos(2*x*180/pi))+0.5*y*y*4};
    \end{axis}
  \end{tikzpicture}
\end{document}

nongliding

True, there is a problem with color. The equation in the question is 1/x^5*1/y^3+ 2/x^5*1/y^3+2*y^(9/2) reduced. It's very sharp and using 256 samples for example instead causes main memory to run out and TeX halts compiling... Smooth with around 100 samples and rotating the view may be the only option, when used with a compatible color scheme to return the surface fill.

  • smooth is not a good idea for closed form plotting. Not only it makes the plot wrong but also often introduces wrong color schemes – percusse May 10 '15 at 17:31
2

Just for showing another plotting approach, here is the LaTeX-R-knitr solution.

\documentclass[10pt,letterpaper]{article}

\begin{document}

\begin{figure}[scale=2.5]
<<>>=
library(lattice)
param<-2
x<-seq(0,1,len=30)
y<-seq(0,1,len=30)
g<-expand.grid(x=x,y=y)
g$z<-(g$x^(-param-1)*g$y^(-param-1)*(g$x^(-param)
     +g$y^(-param)-1)^(-1/param-2)+param*g$x^(-param-1)*g$y^(-param-1)*(g$x^(-param)
     +g$y^(-param)-1)^(-1/param-2))
wireframe(z~x*y,g,drape=TRUE,aspect=c(1,1),colorkey=TRUE
          ,screen = list(x=-40,y=-60,z=-45))
@
%
\end{figure}
\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.