TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

What I want to do is plot a graph for a given data set. This must be simple to do, but I just cannot find a simple explanation of how to do it...

I have the following table (no problem so far):

\hline % Horizontal overbar
X & Y \\\hline
0 & 0 \\
1 & -40.0975460694 \\
2 & -46.1181459826 \\
3 & -49.6399711638 \\
4 & -52.1387458959 \\
5 & -54.0769461561 \\
6 & -55.660571077 \\
7 & -56.9995068696 \\
8 & -58.1593458092 \\
9 & -59.1823962581 \\
10 & -60.0975460694 \\
\caption{An example table.}

I want to plot the data in the second column against those in the first one. How can I do that?


\addplot table[x = MyX] from testdata.txt;
\addplot table[y = MyY] from testdata.txt;


0   0
1   -40.0975460694
2   -46.1181459826
3   -49.6399711638
4   -52.1387458959
5   -54.0769461561
6   -55.660571077
7   -56.9995068696
8   -58.1593458092
9   -59.1823962581
10  -60.0975460694

I'm getting an error: bad math environment on the line containing \begin[axis].

I do load both the pgfplots and pgfplotstable packages...

share|improve this question
You are not allowed to use any random parentheses. You need to use the correct ones. See the answers here and the TikZ/pgfplot manual. – Hans-Peter E. Kristiansen Jan 12 '14 at 17:06

The combination of »pgfplots« and »pgfplotstable« is your friend here. Together with »array« and »booktabs« for enhanced formatting you get both a nice table and a nice plot.

The advantage is that if you modify the data file, the modifications will be respected in the table as well as the plot. Further customization is of course possible (see package manuals).


  empty cells with={---},
  every head row/.style={before row=\toprule,after row=\midrule},
  every last row/.style={after row=\bottomrule}


0 0
1 -40.0975460694
2 -46.1181459826
3 -49.6399711638
4 -52.1387458959
5 -54.0769461561
6 -55.660571077
7 -56.9995068696
8 -58.1593458092
9 -59.1823962581
10 -60.0975460694

    \caption{Data table}
      sci zerofill,
      columns/X/.style={column type=r},
      columns/Y/.style={dec sep align}

        \addplot table {data.txt};
    \caption{Data plot}

The »filecontents« package with its corresponding filecontents* environment are only used to create the data file in order to make this example self-contained. You won't need them in the actual document.

enter image description hereenter image description here

share|improve this answer

Use an input file with just your numbers

0 0
1 -40.0975460694

Then use the pgfplots and pgfplotstable packages.

ok, let me edit my answer to reflect your edited question:

Use curly brackets, not square ones. Also, in your input file use # in front of myx myy because they're not data.

\addplot file{testdata.txt};
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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