1

I am a very beginner of Tikz/pgf. I have searched quite long but didn't found the answer. How could I plot from an external txt or dat file that with one X value and several Y values? For example, the external txt file could be like this,

enter image description here

Many thanks!

3

It is easy using y index with pgfplots.

\documentclass{article}
\usepackage{pgfplotstable}
\pgfplotstableread{
x y1 y2 y3 y4
0   3   4  13   14
1   5   6  15   16
2   2   3  12   13
3   4   8  14   18
4   6   9  26   19
5   8   1  18   11
6   10  2  12  12
}\mytable
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot table [x index=0, y index=1]{\mytable};
\addplot table [x index=0, y index=2]{\mytable};
\addplot table [x index=0, y index=3]{\mytable};
\addplot table [x index=0, y index=4]{\mytable};
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

If you have valid column names you can simply use

\addplot table [y=y1]{\mytable};
\addplot table [y=y2]{\mytable};
\addplot table [y=y3]{\mytable};
\addplot table [y=y4]{\mytable};
  • Why y index? Columns have pretty legal names. – percusse Jan 27 '15 at 0:41
  • @percusse Just to be more general!.. – user11232 Jan 27 '15 at 0:42
  • @percusse OK, I edited. – user11232 Jan 27 '15 at 0:56
  • @HarishKumar thank you so much! Another question is how can I read the external txt file. I would like to draw directly from the txt file not to copy them to tex editor. – iori266 Jan 28 '15 at 6:47
  • @iori266 Hi. Glad it was useful. Please remember that you can upvote and accept the answers. For any clarifications, please refer: How do you accept an answer – user11232 Jan 28 '15 at 6:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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