[Tex/LaTex] pgfplots multiple plots in one file

pgfplotspgfplotstable

In pgfplots is it possible to have several plots in one datafile?

The goal is to add about 20 plots, which has different number of points and not the same x-value, to the same axis. (app. 350 points)


One posible solution is to have the plots in separate column, and then use loads of 'nan' because of the different x values:

    x  p1   p2  P3  ...
    1  3    nan nan ...
    2  nan  5   nan ...
    6  4    8   nan ...
    8  7    nan nan ...
    ...

I do not like this solution:o(


The gnuplot solution is nice: the different plots are separated in the datafile by two newlines, and they are selected by 'index'. Does something similar exist in pgfplots? or any other solution?

Best Answer

You can use gnuplot in the background by using \addplot gnuplot [raw gnuplot] {<gnuplot commands>};:

\documentclass{article}
\usepackage{pgfplots}
\usepackage{filecontents}

\begin{filecontents*}{data.txt}
1 1
2 4
3 9
4 16
5 25


1 1
2.5 5
6 12
\end{filecontents*}


\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot gnuplot [raw gnuplot] {plot 'data.txt' index 0};
\addplot gnuplot [raw gnuplot] {plot 'data.txt' index 1};
\end{axis}
\end{tikzpicture}
\end{document}