pgfplots multiple plots in one file

You can use the \addplot command multiple times on the same axes. The files that the different \addplots take can be the same or different, and different columns etc.

\pgfplotsset{width=6cm,compat=newest}
\begin{axis}[
    scale only axis,
    xlabel={$\tau$ ($\mu$s)},
    ylabel={P(bitflip)},
    ymin=0,
    ymax=1,
    xmin=0,
    xmax=125.66,
]
\addplot[color=red]
    table[x=time,y=fidelity] {data1.txt};
\addplot[color=black]
    table[x=time,y=fidelity] {data1.txt};
\end{axis}
\end{tikzpicture}

It's not so clear here, but I used data from two sample files to plot this

enter image description here

The data files here are indexed by column headers, but you can also do this by number. One of the files begins with the following lines:

time    fidelity
0.00000000e+00  0.00000000e+00
2.02874597e-01  9.75413405e-05
5.19896845e-01  4.76482130e-04
8.36998426e-01  7.26895359e-04
1.15410184e+00  7.52986468e-04
1.47115744e+00  8.85559658e-04

You can put multiple columns in one file, and gaps (just whitespace) are tolerated. In the following plot one data file is used with four columns, and there are clear gaps in the time and amplitude entries for the red plot.

enter image description here

\pgfplotsset{width=6cm,compat=newest}
\begin{tikzpicture}
\begin{axis}[
    scale only axis,
    xlabel={$\tau$ (ns)},
    ylabel={Negativity},
    ymin=-1,
    ymax=1,
    xmin=0,
    xmax=6.3,
    legend style={font=\small,at={(0.5,0.96)},anchor=north,style={nodes={right}}},
]
\addplot[color=black]
    table[x index=0,y index=1] {data.txt};
\addplot[color=red]
    table[x index=2,y index=3] {data.txt};
\end{axis}
\end{tikzpicture}

Note that there are no column headers this time (just for variety). Gaps look like

2.28479466e+00  7.55749574e-01  -6.54860734e-01 2.28479466e+00  
2.34826118e+00  7.12694171e-01  -7.01474888e-01 2.34826118e+00  
2.41172769e+00  6.66769001e-01  -7.45264450e-01 2.41172769e+00  
2.47519421e+00  6.18158986e-01      
2.53866073e+00  5.67059864e-01      
2.60212725e+00  5.13677392e-01      
2.66559377e+00  4.58226522e-01      
2.72906028e+00  4.00930535e-01      
2.79252680e+00  3.42020143e-01      
2.85599332e+00  2.81732557e-01      
2.91945984e+00  2.20310533e-01      
2.98292636e+00  1.58001396e-01      
3.04639288e+00  9.50560433e-02      
3.10985939e+00  3.17279335e-02      
3.17332591e+00  -3.17279335e-02     
3.23679243e+00  -9.50560433e-02     
3.30025895e+00  -1.58001396e-01     
3.36372547e+00  -2.20310533e-01     
3.42719199e+00  -2.81732557e-01     
3.49065850e+00  -3.42020143e-01     
3.55412502e+00  -4.00930535e-01     
3.61759154e+00  -4.58226522e-01 -8.88835449e-01 3.61759154e+00  
3.68105806e+00  -5.13677392e-01 -8.57983413e-01 3.68105806e+00  
3.74452458e+00  -5.67059864e-01 -8.23676581e-01 3.74452458e+00  
3.80799110e+00  -6.18158986e-01 -7.86053095e-01 3.80799110e+00

It should also be fine to have uneven columns, provided the number of tabs or commas is correct.


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}