2

Consider a data file on this format:

x y
0 2
1 3
2 4
0 3
1 5
2 6
0 5
1 7
2 8

The first column repeats it self three times ([0 1 2]) and there are three different data sets here.

A more convenient way to process the file with pgfplots would be in the format:

x y z w
0 2 3 5
1 3 5 7
2 4 6 8

But the software I am using spits out the data in the first format. Is it possible to plot the data on the first format and end up with the same plot as in the following mwe?

\documentclass{standalone}
\usepackage{pgfplots}
\usepackage{filecontents}
\begin{filecontents}{data.txt}
x y z w
0 2 3 5
1 3 5 7
2 4 6 8
\end{filecontents}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot table [y=y] {data.txt};
\addplot table [y=z] {data.txt};
\addplot table [y=w] {data.txt};
\end{axis}
\end{tikzpicture}
\end{document}

Desired output:

desired_output

Here's the code with the data on the other format (and the default result):

\documentclass{standalone}
\usepackage{pgfplots}
\usepackage{filecontents}
\begin{filecontents}{data.txt}
x y
0 2
1 3
2 4
0 3
1 5
2 6
0 5
1 7
2 8
\end{filecontents}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot table [y=y] {data.txt};
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

Something along the lines of "while x is increasing, continue plot, else start new plot" would do the trick..

1 Answers1

3

You can use skip coords index={<begin>}{<end>} as detailed in How to select a finite number of samples from the file when plotting using pgfplot, for example

Here's a complete example:

\documentclass{standalone}
\usepackage{pgfplots}
\usepackage{filecontents}
\begin{filecontents}{data.txt}
x y
0 2
1 3
2 4
0 3
1 5
2 6
0 5
1 7
2 8
\end{filecontents}
\begin{document}
\begin{tikzpicture}
\begin{axis}
  \addplot table [skip coords between index={3}{9},y=y] {data.txt};
  \addplot table [skip coords between index={0}{3},
                  skip coords between index={6}{9},y=y] {data.txt};
  \addplot table [skip coords between index={0}{6},y=y] {data.txt};
\end{axis}
\end{tikzpicture}
\end{document}
cmhughes
  • 100,947
  • Thanks! Yeah this could work in this case as I know the length of the x-vector. It might however be of unknown length in other cases and it would be a bit tedious to manually find the length every time the data is updated. I'll accept the answer unless some other (a bit less manual) pops up –  Dec 11 '17 at 08:58