# pgfplots with gnuplot: 3d-surface/mesh plot and problems with number of samples

I want to plot a 3d-surface/mesh of a given function via 'gnuplot'. Of course the smoothness of the surface depends on the number of samples. But here I run into a small problem. Take the following example:

\documentclass{scrartcl}

\usepackage{tikz}
\usepackage{pgfplots}

\pgfplotsset{compat=1.11}

\begin{document}

\begin{figure}
\centering

\begin{tikzpicture}
\begin{axis}[
grid=both,
view={-45}{90},
xmin=0,
xmax=6.28,
ymin=0,
ymax=6.28,
zmin=0,
zmax=1,
xlabel=$x$,
ylabel=$y$,
]
mesh,
draw = black,
line width = 0.5pt,
samples = 20,
samples y = 20,
raw gnuplot
] gnuplot
{set samples 20,20; set isosamples 20,20 ;splot [0:2*pi] [0:2*pi] cos(x)*cos(y)};
\end{axis}
\end{tikzpicture}

\end{figure}

\end{document}


As far as I understand it, in gnuplot 'set isosamples (ix),(iy)' sets the number of lines perpendicular to the x,y axis, on which the function is evaluated. 'set samples (x),(y)' sets the number of points on these lines (in x,y direction), where the function is evaluated.

My problem is that pgfplots seems to have a problem with these two kind of sample numbers (isosamples and samples).

I already found out, that the 'samples' and 'samples y' options of \addplot3 are disabled by the use of 'raw gnuplot'. So I guess I can just ignore them.

By testing I came to the conclusion that the number of samples for the pgfplot is determined by (x) (the x value in 'set samples') and by (iy) (the y value in 'set isosamples'). The values of (y) and (ix) do not mater at all. This means that basically I can use 'set samples (x),2' and 'set isosamples 2,(iy)', where (x),(iy) specify my desired sample size for the output.

So here are my questions:

• Did I get this right? Can anyone confirm this and maybe explain to me why it is that way?
• Is there a way to exactly reproduce the outputs I would get from gnuplot (using isosamples and samples) and pgfplots? I think the main difference here is that pgfplot uses a equidistant mesh, while gnuplot differentiates between in the number of lines and number of samples points on these lines.
• pgfplots does a good job at sampling the patches: your minimal example could be done with builtin sampling routines of pgfplots without gnuplot. However, it has no feature which does the job of isosamples (see @Pouya's answer below), not even if combined with raw gnuplot and gnuplot's isosamples key. Nov 7, 2014 at 21:46
• Thanks for the comment and the clarification. I know that this example could also be done with pgfplots, but I want to use gnuplot for more complicated functions (inverse trig. fuctions). Nov 10, 2014 at 7:59

I will try to shed some light here, hope it will help:

Regarding samples and samples y in \addplot3, 4th chapter of pgfplots documentation explains the issue clearly:

The samples key defines the number of samples used for line plots while the samples y key is used for mesh plots.

that means in your case you need to specify samples y. With that said, pay attention that use of raw gnuplot

Disables the use of samples and domain

To clarify the confusion in samples and isosamples in gnuplot, here are some visual clues. Note that to exclude pgfplots' effects, I have produced these via gnuplot's command line:

set samples 20,20; set isosamples 20,20 ;splot [0:2*pi] [0:2*pi] cos(x)*cos(y)


set samples 5,5; set isosamples 20,20 ;splot [0:2*pi] [0:2*pi] cos(x)*cos(y)


set samples 20,20; set isosamples 20,2 ;splot [0:2*pi] [0:2*pi] cos(x)*cos(y)


So, set isosamples determines the isoline density of surfaces.

An isoline is a curve parametrized by one of the surface parameters while the other surface parameter is fixed. Isolines are a simple means to display a surface. By fixing the u parameter of surface s(u,v), the iso-u lines of the form c(v) = s(u0,v) are produced, and by fixing the v parameter, the iso-v lines of the form c(u) = s(u,v0) are produced.

For your second question, I'm afraid I'm not sure what you mean exactly. Using gnuplot command employs the external program gnuplot to compute coordinates. If you want to have results similar to gnuplot, I think using raw gnuplot, you are in the correct track.

• Great illustration of the difference between samples and isosamples. NB: this kind of fails if set hidden3d is used... Is there a similar mechanism when the splot is based on a datafile? i.e. data is at high resolution, and could be used to plot e.g. a colored surface with pm3d, but meshlines would be shown only every 10 or 20 gridlines? In other words, how to add high resolution contour lines along x and y (set contour only does it along z)? May 27, 2015 at 14:25
• @pybuen, I'm not sure, I have to play around. Do have a sample data for test? If it is not confidential give a link here. May 27, 2015 at 14:43
• Thanks! See there a self-contained gnuplot script with inline binary data. Load it in gnuplot (load "sample.gp") for the plot to be interactive / or cat+pipe it in gnuplot. See the variables downsampling_ratio_x and downsampling_ratio_t at the start and the several 3D plot alternatives to comment in/out. Ideally, I would like to superimpose alternatives 0a and 1b, i.e. having high resolution pm3d plot but showing much more spaced meshlines, however without compromising the resolution of these meshlines! May 28, 2015 at 11:37