3

I have a single column .txt file with the following info in it

1
2
3
4
5

I use the following ME and it does no work:

\documentclass[border=1]{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.5}
\begin{document}
\begin{tikzpicture}
    \begin{axis}
            \addplot table {TestTable.txt};
     \end{axis}
\end{tikzpicture}
\end{document}

where TestTable.txt contains as a single column the above numbers.

I would like to plot this using pgfplots (this is just an example so the result is a trivial line). The result should be a straight line of course (e.g. y=x). Hopefully the answer should also be applicable to a larger sample.

5
  • It's not clear to me why you expect that straight line to be the result. How are pair of points produced from your single column table? Oct 13, 2015 at 16:15
  • the "x values" should be from 1 to 5 hence I expect a tilted straight line. Oct 13, 2015 at 16:17
  • "1 to 5" as in the row number or the value read in the column? In the latter case the correct answer is the one from Gonzalo
    – LucaD
    Oct 13, 2015 at 16:20
  • @ShutupandCalculate Please, next time state your problem clearly and correctly so people don't have to guess what you want. Oct 13, 2015 at 16:23
  • @GonzaloMedina OK fair enough. I should have been more clear. Sorry. Oct 13, 2015 at 16:24

1 Answer 1

4

To use the row index as x coordinate you can use the key x expr together with the macro \coordindex (but since this starts from 0, you must add 1). Then, to use the value in the single column as y coordinate, you need y index=0. Complete code:

\documentclass[border=1]{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.5}
\begin{document}
\begin{tikzpicture}
    \begin{axis}
            \addplot table [x expr=\coordindex+1, y index=0]
              {TestTable.txt};
     \end{axis}
\end{tikzpicture}
\end{document}
1
  • Hmmmm. I think probably this is more what OP was expecting. I'll delete my answer. Oct 13, 2015 at 16:22

Not the answer you're looking for? Browse other questions tagged .