# pgfplots / gnuplot: How to draw a 3D two-parametric function

I want to plot a 2-parametric surface function

set parametric
splot cos(u)*cos(v),sin(u)*cos(v),sin(v)


(Hint: If I just put it into gnuplot it works.)

I guess that special settings in pgfplots are necessary for such a special function.

So: Why does the 2nd example plot nothing?
Which options do I have to set here in pgfplots?

% arara: pdflatex: {shell: yes}

\documentclass[margin=3mm, tikz]{standalone}
\usepackage{pgfplots}
\begin{document}

\begin{tikzpicture}
\begin{axis}[title=Works]
gnuplot{sin(x)*sin(y)};
\end{axis}
\end{tikzpicture}

\begin{tikzpicture}[font=\footnotesize,]
\begin{axis}[title={Works not - what do I have to set?}]
gnuplot {
set parametric
splot cos(u)*cos(v),sin(u)*cos(v),sin(v)
};
\end{axis}
\end{tikzpicture}
\end{document}
\end{document}


Should give sth. like this:

• When you use external programs like gnuplot or sage you need to run the external program as well and then compile a second time in LaTeX. Have you done that? If so what does the .log file tell you? If you haven't run gnuplot then your .log file will say something about the current plot has no coordinates or have been filtered away. See my comment here for someone with similar problem.
– DJP
Aug 21 '20 at 22:15
• @DJP My problem is not that it never works with gnuplot, but that it doesn't work with gnuplot here. I didn't say anything like that either, on the other hand I didn't say it wasn't said either. So please, see my edit.
– cis
Aug 22 '20 at 3:24
• @DJP The problem is probably not the compilation, but the settings in pgfplots or gnuplot. I assumed it was obvious - my fault. Have you tested the example and does it output something - I don't think so, otherwise you would not have written general information about the gnuplot compilation process.
– cis
Aug 22 '20 at 3:31
• In my comment, I asked (provided you were compiling properly) for what your log file indicates. On my computer, the log file says: "PGFPlots: reading {STEX.pgf-plot.table} Package pgfplots Warning: the current plot has no coordinates (or all have been filtered away) on input line 19. [warning /pgfplots/warning/plot without coordinates] Package pgfplots Warning: You have an axis with empty range (in direction z). Replacing it with a default range and clearing all plots. on input line 20.". My gnuplot version is 5.0 (current version is 5.4) so your log could be different.
– DJP
Aug 22 '20 at 16:28

You get a .gnuplot file that you can look at to see which code is being passed to Gnuplot.

set table "test.pgf-plot.table"; set format "%.5f"
set format "%.7e";; set samples 25, 25; set dummy x,y; set isosamples 25, 25; splot [x=-5:5] [y=-5:5]  set parametric splot cos(u)*cos(v),sin(u)*cos(v),sin(v) ;


As you can see you get splot <domain settings> <your code>, meaning that Gnuplot gets

splot ... set parametric


which doesn't work because after splot and options there should be a function expression. You can also see the error from Gnuplot in the .log file, it says

"test.pgf-plot.gnuplot" line 2: undefined variable: set


where the set in question is that in set parametric.

To fix this you can use the parametric key on the TikZ side of things instead of set parametric:

% arara: pdflatex: {shell: yes}
\documentclass[margin=3mm, tikz]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}[font=\footnotesize,]
\begin{axis}[title={Works not - what do I have to set?}]
gnuplot [parametric=true] {
cos(u)*cos(v),sin(u)*cos(v),sin(v)
};
\end{axis}
\end{tikzpicture}
\end{document}


Or you can use raw gnuplot. In that case you need to set the domain and sample size in the Gnuplot code instead of the pgfplots being passed on:

% arara: pdflatex: {shell: yes}
\documentclass[margin=3mm, tikz]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}[font=\footnotesize,]
\begin{axis}[title={Works not - what do I have to set?}]
gnuplot [raw gnuplot] {
set parametric;
splot cos(u)*cos(v),sin(u)*cos(v),sin(v)
};
\end{axis}
\end{tikzpicture}
\end{document}

• Looking good. Is there a way to get better help- / surface-lines?
– cis
Aug 23 '20 at 13:23
• @cis Don't really know. The domain and sample size affects the look and number of the lines though, so you can try fiddling with that. (For the first version, e.g. \addplot3[samples=32, samples y=10,domain=0:2*pi] ..., for the second one whatever Gnuplot the settings are for changing those things.) Aug 23 '20 at 13:32
• I think, the second thing is the problem: find good gnuplot options. pgfplots-gnuplot does not create the same picture as plain-gnuplot does.
– cis
Aug 23 '20 at 15:57
• @cis Well you shouldn't necessarily expect that either. pgfplots only uses Gnuplot to calculate a file with values, the visualization of those values is entirely done by pgfplots. Aug 23 '20 at 16:00