[Tex/LaTex] Plots with data coming from different files

pgfplotstikz-pgf

Is it possible to draw a graph with pgfplots using data from
different files ?

Let say I have two data files : data.dat and error.dat and would
like to plot column 1 versus column 0 from data.dat and add error
bars from error.dat.

If the data all came from the same file all.dat, I would use :

\pgfplotstableread{./all.dat}\mytable

\addplot  table[x index=0,y index=1, y error index=3]{\mytable};

Best Answer

It is possible. You can make a column from a different table available on the fly using

\pgfplotstableset{
    create on use/<new column name>/.style={
        create col/copy column from table={<file or table>}{<column name>}
    }
}

So, to get something like

you could do something like

\documentclass{article} 
\usepackage{filecontents}
\begin{filecontents}{data.dat}
X   Y
1   3
2   7
3   4
4   5
5   5
6   9
\end{filecontents}

\begin{filecontents}{error.dat}
Errors
1
3
2
2.5
3
1
\end{filecontents}

\usepackage{pgfplots,pgfplotstable}

\pgfplotstableread{data.dat}\datatable

\pgfplotstableset{
    create on use/errors/.style={
        create col/copy column from table={error.dat}{Errors}
    }
}


\begin{document}
\begin{tikzpicture}
\begin{axis}[xmin=0,ymin=0]
\addplot +[
    black,
    error bars/.cd,
        y explicit,
        y dir=both
    ] table [y error=errors] {\datatable};
\end{axis}
\end{tikzpicture}

\end{document}
Related Question