Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
share|improve this question
1  
Usually the table describes (x,y) coordinates and so sorting would not change the plot. What are you trying to achieve? –  canaaerus Jul 20 '12 at 4: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 5:20
add comment

1 Answer

up vote 7 down vote accepted

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}

share|improve this answer
    
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 7:03
    
You should open a feature request for that at sourceforge.net/tracker/?group_id=224188&atid=1060659. In principle, it shouldn't be too hard to do. –  Jake Jul 20 '12 at 7:09
1  
done, sourceforge.net/tracker/…. –  alfC Jul 20 '12 at 7:16
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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