6

Is it possible to sort a table at the point that is included in a pgfplot. In the example below, the point x=3 and x=4 are ordered for plotting. I am looking prefereably for an 'in-place' solution, rather than a convoluted solution using pgfplotstable.

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\begin{document}
    \begin{tikzpicture}
    \begin{axis}
        \addplot table[x=x, y=y] { %something like table[sort index=#1]?)
            x   y
            1   11
            2   14
            4   26
            3   39
        };
    \end{axis}
    \end{tikzpicture}
\end{document}
alfC
  • 14,350
  • 1
    Usually the table describes (x,y) coordinates and so sorting would not change the plot. What are you trying to achieve? – bodo Jul 20 '12 at 04:57
  • I want to plot the (x,y) points but sorted with respect to x. So the points are connected in this order (1,11)-(2,14)-(3,39)-(4,26) and not as (1,11)-(2,14)-(4,26)-(3,39). Of course this was an example data points, the interesting case is when the table is comming from a large file with many points. The effect I want is similar to smooth unique option in gnuplot, as in the gnuplot command line: plot 'data.txt' smooth unique with lines. – alfC Jul 20 '12 at 05:20

1 Answers1

10

If you don't mind using gnuplot as the backend to PGFPlots, you can use the smooth unique option you mentioned. That's actually quite a good way to do it, because gnuplot is much faster at sorting than PGFPlotstable.

\documentclass{standalone}
\usepackage{pgfplots}

\usepackage{filecontents}
\begin{filecontents}{testdata.dat}
x   y
1   11
2   14
4   26
3   39
\end{filecontents}


\begin{document}
    \begin{tikzpicture}
    \begin{axis}
        \addplot gnuplot [raw gnuplot] {plot 'testdata.dat' using 1:2 smooth unique;};
    \end{axis}
    \end{tikzpicture}
\end{document}

Otherwise, I don't think you'll get around using a preprocessing step, either using pgfplotstable or some other tool. As Christian Feuersänger said here, the sort option only works for typesetting tables, but not for plotting. The solution wouldn't be very "convoluted", however, all it takes is one line of code to sort the table and store the result in a new macro:

\documentclass{standalone}
\usepackage{pgfplots, pgfplotstable}

\usepackage{filecontents}
\begin{filecontents}{testdata.dat}
x   y
1   11
2   14
4   26
3   39
\end{filecontents}

\pgfplotstablesort{\sorted}{testdata.dat}

\begin{document}
    \begin{tikzpicture}
    \begin{axis}
        \addplot table {\sorted};
    \end{axis}
    \end{tikzpicture}
\end{document}

Jake
  • 232,450
  • Actually, I was looking to replace the gnuplot backend. The second option seems to be the simplest option until a sort option is implemented (!) for the pgfplot table object. Thanks. – alfC Jul 20 '12 at 07:03
  • You should open a feature request for that at http://sourceforge.net/tracker/?group_id=224188&atid=1060659. In principle, it shouldn't be too hard to do. – Jake Jul 20 '12 at 07:09
  • 1
    done, https://sourceforge.net/tracker/?func=detail&aid=3546239&group_id=224188&atid=1060659. – alfC Jul 20 '12 at 07:16