# Create one linear line plots from each row of data in table

I have a table where the data has the following format:

| x    | y    | dydx  |
|------|------|-------|
| x1   | y1   | dydx1 |
| x2   | y2   | dydx2 |
| x3   | y3   | dydx3 |


I want to create a plot with multiple linear lines with the following expression: y(x_sub) = y_i + dydx_i * (x_sub-x_i)

where, i is the row number in the table and x_sub is the range [x_i - a, x_i + a], where a is some specified value. So, each line will be centered at x_i. I have attached a plot (generated with Matlab) that shows the green and black lines centered at x_i and use the y and dydx values in the table.

I am using pgfplot to create my plots. I would greatly appreciate any guidance on this. Thanks!

• Welcome to TeX.SX! It's not clear to me what you want. What do you mean by x_sub = x_i +/- 10%? Also why the lines should be centered by this is not clear to me. Could you provide an example plot or be more precise please?
– nox
Jul 5, 2018 at 21:49
• Sorry about the ambiguity. I have modified the post and added a figure to make it more descriptive. I hope this will be more specific. Thanks! Jul 5, 2018 at 22:04

If I understood you correctly, this might be something like what you're after.

%%%%%%%%
%% the following only for example
\RequirePackage{filecontents}
% the filecontents environment writes its content to the specified file
\begin{filecontents*}{data.dat}
x y dydx
0 2 1
1 1 -1
2 3 0.5
\end{filecontents*}
%%%%%%%

\documentclass[border=5mm]{standalone}
\usepackage{pgfplotstable}
\begin{document}
\begin{tikzpicture}
% get number of rows in table
% subtract 1 because row indexing starts at zero
\pgfplotstablegetrowsof{\mydata}
\pgfmathtruncatemacro{\NumRows}{\pgfplotsretval-1}

\begin{axis}[
xmin=-5,
xmax=5,
samples=2
]

\pgfmathsetmacro{\AxRange}{\pgfkeysvalueof{/pgfplots/xmax}-\pgfkeysvalueof{/pgfplots/xmin}}

\pgfplotsinvokeforeach{0,...,\NumRows}{ % loop over rows

% extract the data from the table
\pgfplotstablegetelem{#1}{x}\of\mydata % x is column name
\pgfmathsetmacro{\X}{\pgfplotsretval}
\pgfplotstablegetelem{#1}{y}\of\mydata
\pgfmathsetmacro{\Y}{\pgfplotsretval}
\pgfplotstablegetelem{#1}{dydx}\of\mydata
\pgfmathsetmacro{\DYDX}{\pgfplotsretval}

% calculate start and end of domain for line
\pgfmathsetmacro{\DomainStart}{\X-\AxRange*0.1}
\pgfmathsetmacro{\DomainEnd}{\X+\AxRange*0.1}

% plot
\addplot +[domain=\DomainStart:\DomainEnd,mark=none,thick] {\Y + \DYDX * (x-\X)};
}

\end{axis}
\end{tikzpicture}
\end{document}

• Awesome! A couple of questions: I will have several such plots in my document and the data for each plot is in an independent file. So, can I modify the \pgfplotstableread command to read the data files and move it between the \begin{tikzpicture} ... \end{tikzpicture}? Thanks! Jul 5, 2018 at 22:30
• One more question: If I use \begin{axis} [xmin=0.1,xmax=0.2] ... code ... \end{axis}, is there an equivalent of \pgfmathsetmacro{\AxRange}{\XMax-\XMin} to get the value of \AxRange from xmin and xmax? Jul 5, 2018 at 22:38
• @user6593 \pgfplotstableread{filename}\mydata. Assuming the columns are white-space delimited. And \pgfmathsetmacro{\AxRange}{\pgfkeysvalueof{/pgfplots/xmax}-\pgfkeysvalueof{/pgfplots/xmin}} inside the axis. Jul 6, 2018 at 5:54
• @user6593 I edited the code accordingly. Jul 6, 2018 at 5:58
• Thanks! I get the following error while compiling this source. ./a.tex:52: Undefined control sequence. \sa@placebox ->\newpage \global \pdfpagewidth =\wd \sa@box \global \pdfpageh... l.52 \end{document} ./a.tex:52: You can't use a prefix with the character =. <to be read again> = l.52 \end{document} ./a.tex:52: Missing number, treated as zero. <to be read again> \global l.52 \end{document} I am using MacTex 2017 with pgfplots 1.14. Could this be the issue? Jul 6, 2018 at 11:12