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!

enter image description here

  • 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 '18 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!
    – user6593
    Jul 5 '18 at 22:04

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

enter image description here

%% the following only for example
% the filecontents environment writes its content to the specified file
x y dydx
0 2 1
1 1 -1
2 3 0.5

% get number of rows in table
% subtract 1 because row indexing starts at zero



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

  % extract the data from the table
  \pgfplotstablegetelem{#1}{x}\of\mydata % x is column name

  % calculate start and end of domain for line

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

  • 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!
    – user6593
    Jul 5 '18 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?
    – user6593
    Jul 5 '18 at 22:38
  • 1
    @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 '18 at 5:54
  • @user6593 I edited the code accordingly. Jul 6 '18 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?
    – user6593
    Jul 6 '18 at 11:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.