0

Is it possible to get and store a specific y value from a plot coordinate, so that it can be printed somewhere else (for example inside a node)?

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\usepgfplotslibrary{fillbetween}
\begin{filecontents}{data-fake5.dat}
x       y  
1       0  
2       3  
3       1  
\end{filecontents}
\begin{document}
\begin{tikzpicture}   
\begin{axis}[
axis x line*=bottom,
axis y line*=left, 
]
\addplot[name path=myplot] table[x=x,y=y] {data-fake5.dat};

%\node at (1,1.5){\printy{myplot}{2}}; %this should print the y value of the plot 'myplot` associated with x=2, which is 3 \end{axis}
\end{tikzpicture} \end{document}

desired result:

enter image description here

d-cmst
  • 23,095
  • You can use \pgfplotstablegetelem (pgfplotstable package). Alternately, you can extract the coordinate screen location (in pts) using \pgfgetlastxy or \pgfextracty and recompute the axis cs value.. – John Kormylo Oct 13 '20 at 13:45
  • yup, that (pgfplotstablegetelem) was what i was looking for. Thank you very much. I would gladly accept your answer, should you provide one. – d-cmst Oct 13 '20 at 15:00

1 Answers1

1

The only tricky bit is that rows are numbered starting with 0, and the head (x y) doesn't count.

\documentclass{standalone}
\usepackage{pgfplotstable}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\usepgfplotslibrary{fillbetween}
\begin{filecontents}{data-fake5.dat}
x       y  
1       0  
2       3  
3       1  
\end{filecontents}
\begin{document}
\pgfplotstableread{data-fake5.dat}\data
\begin{tikzpicture}   
\begin{axis}[
axis x line*=bottom,
axis y line*=left, 
]
\addplot[name path=myplot] table[x=x,y=y] {\data};
%\pgfplotsextra{\pgfplotstablegetelem{1}{y}\of{\data}}% alternate location
%\node at (1,1.5) {\pgfplotsretval};
\node at (1,1.5) {\pgfplotstablegetelem{1}{y}\of{\data}\pgfplotsretval};
\end{axis}  
\end{tikzpicture} 
\end{document} 
John Kormylo
  • 79,712
  • 3
  • 50
  • 120