5

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?

1 Answer 1

8

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .