# How can I produce a graph from a table?

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):

\begin{table}
\centering
\begin{tabular}{|c|c|}
\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 \\
\end{tabular}
\caption{An example table.}
\end{table}


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

Edit:

\begin[tikzpicture]
\begin[axis]
\addplot table[x = MyX] from testdata.txt;
\addplot table[y = MyY] from testdata.txt;
\end[axis]
\end[tikzpicture]


testdata.txt

MyX MyY
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...

-
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 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).

\documentclass[11pt]{article}
\usepackage[T1]{fontenc}
\usepackage{caption}

\usepackage{pgfplotstable}
\pgfplotstableset{
empty cells with={---},
every last row/.style={after row=\bottomrule}
}
\pgfplotsset{compat=1.9}

\usepackage{array,booktabs}

\usepackage{filecontents}
\begin{filecontents*}{data.txt}
X Y
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
\end{filecontents*}

\begin{document}
\begin{table}[!ht]
\caption{Data table}
\label{tab:data}
\centering
\pgfplotstabletypeset[
sci zerofill,
precision=3,
columns/X/.style={column type=r},
columns/Y/.style={dec sep align}
]{data.txt}
\end{table}

\begin{figure}[!ht]
\centering
\begin{tikzpicture}
\begin{axis}
\end{axis}
\end{tikzpicture}
\caption{Data plot}
\label{fig:data}
\end{figure}
\end{document}


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.

-

Use an input file with just your numbers

0 0
1 -40.0975460694
.
.
.


Then use the pgfplots and pgfplotstable packages.

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