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.

If I have a table, how can I get the table values and use them as parameters for plotting in pgfplots in gnuplot? In particular, how can multiple curves be plotted with the same functional form from multiple rows?

share|improve this question
add comment

1 Answer 1

up vote 5 down vote accepted

Edit: The first way I did this was with \def commands instead of \pgfmathsetmacro, which does not work.

Approach:

  1. Load the table using \pgfplotstableread
  2. Get the number of rows using \pgfplotstablegetrowsof, and decrement the number by one.
  3. Within the axis, use \foreach to iterate over each row, up to the max value previously found
  4. Use \pgfplotstablegetelem to retrieve the current row and the correct parameters, setting the variables (\a and \b) with \pgfmathsetmacro
  5. Use the variables in the gnuplot command

Code:

\documentclass{standalone}               

\usepackage{pgfplots,pgfplotstable,tikz}
\usepackage{filecontents}

\begin{filecontents}{data.dat}
a b
1 3
2 2
3 1
4 0
5 0.5
3 2
1 9
2 4
\end{filecontents}
\begin{document}

\pgfplotstableread{data.dat}\datafile

\pgfplotstablegetrowsof{\datafile}
\pgfmathsetmacro\adjrows{\pgfplotsretval -1}

\begin{tikzpicture}
\begin{axis}

\foreach \rownum in {0,...,\adjrows}
{
\pgfplotstablegetelem{\rownum}{a}\of{\datafile} 
\pgfmathsetmacro{\a}{\pgfplotsretval}
\pgfplotstablegetelem{\rownum}{b}\of{\datafile} 
\pgfmathsetmacro{\b}{\pgfplotsretval}

\addplot[raw gnuplot] gnuplot{
  set xrange [0:10];
  set samples 100;
  f(x) = \a*x+\b*x*x;
  plot f(x);
};
}
\end{axis}
\end{tikzpicture}

\end{document}

Multiple lines, same functional form

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.