2

I use data1 = Table[] in Mathematica 9 (M9) to generate a table of values. The thing is that when I read the data file, there's only one column! How can I plot this data file in LaTeX using pgfplots? Here's a ME

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

I also tried TestTable.txt instead of .csv but without any success. TestTable.csv is generated in M9 with the following code

Export[NotebookDirectory[] <> "TestTable.csv", data1]

where data1 is defined earlier in M9 is some data e.g. numerical integration or similar. A smaller version of data1 in textform (i.e. TestTable.txt) looks like this

1.1
2.3
3.0
4.1
5.3
6.2
7.3
8.4
9.6
10.3
  • Can you show that data? – user11232 Oct 13 '15 at 15:19
  • Try \addplot table[x expr=\coordindex,y={<column name>}] {TestTable.csv}; – user11232 Oct 13 '15 at 15:20
  • Yes it has Dimensions[data1] = {1000} i.e. 1000 entries of pure numbers. – Your Majesty Oct 13 '15 at 15:21
  • Should I change column name? – Your Majesty Oct 13 '15 at 15:23
  • Can you post that data file so that we can download it? – user11232 Oct 13 '15 at 15:32
1

You can use

\addplot table[x expr=\coordindex,y index=0] {TestTable.csv};

The y values are read from first column which has the index 0 (zeroth column).

Full code:

\documentclass[border=1]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.5}   %% why 1.5? pl. update
\begin{document}
\begin{tikzpicture}
    \begin{axis}
            %\addplot table {TestTable.csv};
            \addplot table[x expr=\coordindex,y index=0] {TestTable.csv};
     \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

The TestTable.csv has the following content:

1.1
2.3
3.0
4.1
5.3
6.2
7.3
8.4
9.6
10.3
  • I have pgfplots version 1.7, is that good enough? – Your Majesty Oct 13 '15 at 18:12
  • @ShutupandCalculate The latest is 1.12 – user11232 Oct 13 '15 at 22:48
  • Ok 1.7 sounded higher to me... – Your Majesty Oct 14 '15 at 7:06
  • @ShutupandCalculate Well, it is bit confusing but look at it this way. 1. is common to all versions. Then you count 3 as 1.3, 4 as 1.4.....9 as 1.9 and then 10 as 1.10.... 12 as 1.12. Hope it is clear. Now the version is 12.1 so it will be 1.12.1 – user11232 Oct 14 '15 at 7:17
  • 1
    @ShutupandCalculate I am a physicist too. :) But it is a convention adopted by Christian (the author of pgfplots). – user11232 Oct 14 '15 at 9:31

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.