Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

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 at 17:06
add comment

2 Answers

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 head row/.style={before row=\toprule,after row=\midrule},
  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}
        \addplot table {data.txt};
      \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.


enter image description hereenter image description here

share|improve this answer
add comment

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.

\begin{tikzpicture}
\begin{axis}
\addplot file{testdata.txt};
\end{axis}
\end{tikzpicture}
share|improve this answer
add comment

Your Answer

 
discard

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.