How to use the results of raw output to create a smooth plot

As by my previous question on plotting an implicit curve I've got some output. But as you can see it is not quite what it should be (apart from your browser's downscaling by a factor 2 of course.

The code used to reproduce this is (run with --shell-escape, of course)

\begin{tikzpicture}[xscale=1,yscale=.25]
\draw plot[id=curve, raw gnuplot] function{
f(x,y) = y**2 + (x**2 - 5)*(4*x**4 - 20*x**2 + 25);
set xrange [-4:4];
set yrange [-15:15];
set view 0,0;
set isosample 1000,1000;
set size square;
set cont base;
set cntrparam levels incre 0,0.1,0;
unset surface;
splot f(x,y)
};
\end{tikzpicture}


I presume gnuplot returns the coordinates of end points of line segments and it's up to TikZ to connect them. But the results are not quite satisfactory because gnuplot returns the segments in a strange order. At least, that's what I assume given the graph. Now, as the documentation on raw gnuplot is rather sparse and there are not much examples of this to be found on the Internet, certainly not with surface plots or implicit functions, I hope someone could give some pointers (or a full-fledged answer, you won't see me complaining).

EDIT: As @Yossi pointed out, the .table file gnuplot creates contains several parts of the curve separated by 2 new lines each. Now TikZ connects these parts but it shouldn't. We're getting there :).

-
I don't know the details of how to do it, but if you would generate enough points, and only plot the points and not the lines between them, would that work well enough for you? –  Yossi Farjoun Oct 31 '10 at 12:34
@Yossi: I would only use this approach if there is no other possibility. –  Hendrik Vogt Oct 31 '10 at 12:48
@Yossi: that kind of ruins the idea of using vector graphics and the result would not be satisfactory, zooming in will break the illusion of the curve. I could use another tool to draw the curve and embed either an image of a vector graphics version of it, but it's fun to push the borders of TikZ a little and do something that hasn't been done before. –  Pieter Oct 31 '10 at 12:56
The problem seems to be that gnuplot generates a bunch of different curves in the .table file, separated by an empty line. gnuplot would like each of these strokes to be drawn separately, without a line between the end of one and the beginning of the other, however, TikZ does draw the connecting line. So a solution would be to find out how to disable that connecting line. –  Yossi Farjoun Oct 31 '10 at 13:20

Based on @Jan's answer, I modified pgfmoduleplot.code.tex such that it now produces a new plot for each segment in the file.

The complete definition of \pgf@readxyfile now reads

\def\pgf@readxyfile{%
\let\par=\pgf@savedpar%
\edef\pgf@temp{\pgf@temp}%
\ifx\pgf@temp\pgfutil@empty%
\else\ifx\pgf@temp\pgf@partext%
\pgfplotstreamstart%
\pgfplotstreamend%
\else%
\expandafter\pgf@parsexyline\pgf@temp\pgf@stop%
\fi\fi%
}


The result of this is the removal of the connecting line segments. The only downside is that the resulting curve is not connected, at 1000 samples in each direction there is one small gap, as can be seen in the screenshot:

I don't know whether this code breaks anything else, but it (almost) solved my problem. Thank you Jan for the tips!

-
That's strange, when I am looking at the data in the .table file, one part of the plot ends with 0.040404 -11.1712 0\n -0.040404 -11.1712 0 and another one starts with -0.040404 -11.1712 0\n -0.0571345 -11.1562 0. I don't quite understand why they don't connect. –  Jan Hlavacek Nov 1 '10 at 22:49
@Jan: Which version of gnuplot? And the parts do connect with me as well, they just show up in the wrong order (so this doesn't happen with consecutive segments, on a first look). It's quite obvious they should connect in the end, they form a connected curve together. Or do you get a connected curve without my patch? –  Pieter Nov 2 '10 at 6:57
@Pieter: Interesting. Now when i actually tried it, I don't get a gap. It connects nicely for me. Using gnuplot 4.4. –  Jan Hlavacek Nov 4 '10 at 17:48
@Jan: I'm using gnuplot 4.2, also known as, the one you get when pulling it from the Ubuntu repositories. I'll try it with 4.4, maybe the problem is already solved in there. –  Pieter Nov 4 '10 at 19:45
@Jan: I've just tried it with gnuplot 4.4 and I still get reorderings in the .table file, hence, only a smooth plot when my "hack" is applied. So to be sure, when you perform gnuplot on the file generated by PGF/TikZ named *.curve.gnuplot (* being the filename of your .tex file) you get a *.curve.table file containing a single contiguous stream of coordinates? –  Pieter Nov 6 '10 at 11:10
show 1 more comment

I don't think it is possible without modifying pgf. The problem is that pgf reads the .table file generated by gnuplot using the \pgfplotxyfile command, which ignores everything except the first two numbers on each line, and ignores empty lines. You would have to change that so that each empty line in the .table file would end current plotstream and start a new plotstream.

I wonder if simply changing the definition of \pgfplotxyfile would be enough, or if that would break anything else. It would be probably safer to define new command, \pgfplotxyfilewithbreaks, and write a new plot handler, based on the gnuplot one but replacing calls to \pgfplotxyfile by \pgfplotxyfilewithbreaks.

-
Do you reckon it could be called a bug? And I'm delving in the pgfplots code as we speak, but there are easier packages to read :). –  Pieter Nov 1 '10 at 17:53
@pieter: I'm not sure that this is a bug, but you might want to submit a patch and a feature request. Explain the situation in which it came up (link to here!) and perhaps the next version of PGF will include this! –  Yossi Farjoun Nov 1 '10 at 23:39
@Yossi It has been fixed now, see sourceforge.net/p/pgf/bugs/149/#e1ce. –  Pieter Jul 31 '13 at 16:59