1

I was looking for this in the pgfplots manual, unfortunately the term "resample" is not mentioned, and "sample" hits the samples key, which refers to analytical functions, not to table data; so I couldn't really find any references.

Consider this MWE:

\documentclass{article}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usetikzlibrary{calc}

\pgfplotstableread[col sep=space,row sep=newline,header=true]{
ts   val
0.0  0.0
1.0  1.0
1.1  4.9
2.0  2.0
2.2  4.9
4.8  0.2
}\mytable

\begin{document}
\begin{tikzpicture}
  \begin{axis}[extra x ticks={3.5}]
\addplot[smooth] table {\mytable};
\addplot[only marks,mark=*,mark options={color=blue}] table {\mytable};
\draw[red] let \p1=(rel axis cs:0,0), \p2=(rel axis cs:1,1), \p3=(axis cs:3.5,0)
  in (\x3,\y1) -- (\x3,\y2);
\end{axis}
\end{tikzpicture}
\end{document}

It results with something like this:

/tmp/test02a.png

Here pgfplots interpolates between the points making a smooth curve, but I'm also interested in the case of linear interpolation. Basically, I'd like to know what is the point that lies on the interpolated curve at, say, x=3.5 (where we do not have a sample/data point), so I can place a node there, and possibly stretch a horizontal line to the y axis.

I gather that pgfplots just does curveto between the points, so it doesn't "know" about any interpolated points in between. So it seems it is possible to use the intersection library for this - but I was wondering if there was an "easier" approach, say through data directly; so I have the coordinates in terms of axis cs, if I also want to label the point with the coordinate value?

2

Drawing the point is easy. Computing the y coordinate is slightly harder. There may be an easier way, but this is the first one I got to work.

\documentclass{article}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usetikzlibrary{calc,intersections}

\newlength{\mydima}
\newlength{\mydimb}
\newlength{\mydimc}

\pgfplotstableread[col sep=space,row sep=newline,header=true]{
ts   val
0.0  0.0
1.0  1.0
1.1  4.9
2.0  2.0
2.2  4.9
4.8  0.2
}\mytable

\begin{document}
\begin{tikzpicture}
  \begin{axis}[extra x ticks={3.5}]
\addplot[smooth,name path=mycurve] table {\mytable};
\addplot[only marks,mark=*,mark options={color=blue}] table {\mytable};
\draw[red,name path=mytick] let \p1=(rel axis cs:0,0), \p2=(rel axis cs:1,1), \p3=(axis cs:3.5,0)
  in (\x3,\y1) -- (\x3,\y2);
\fill [red, name intersections={of=mycurve and mytick}]
 (intersection-1) circle (2pt) node {};
\coordinate (A) at (axis cs:0,0);
\coordinate (B) at (axis cs:1,1);
\end{axis}
\pgfextracty{\mydima}{\pgfpointanchor{A}{center}}
\pgfextracty{\mydimb}{\pgfpointanchor{B}{center}}
\pgfextracty{\mydimc}{\pgfpointanchor{intersection-1}{center}}
\pgfmathparse{(\mydimc-\mydima)/(\mydimb-\mydima)}
\edef\yvalue{\pgfmathresult}
\node[right] at (intersection-1) {\yvalue};
\end{tikzpicture}
\end{document}

intersection

  • Many thanks for that, @JohnKormylo - I think the intersection point can be captured with a \path[name intersections=..] node[...] (mynode) at (intersection-1) {}; idiom (untested); still wondering if there is an easier way to do this (e.g. directly through pgfplotstable data), as I need to do this in a loop... Cheers! – sdaau Jul 5 '14 at 18:29
  • 1
    One alternative is to do your own interpolation (not using curveto), in which case you would have all the x,y coordinates you want. Poilynomial approximation and splines are easy, but tend to overshoot with points like these. – John Kormylo Jul 5 '14 at 20:30

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.