6

Whats the best way of interpolating a graph and show the interpolation like this (using pgfplots): Interpolation
(source: blueleafsoftware.com)

I have the following code for my plot in an axis environment:

\addplot[domain=0:300,samples=5000]{200*0.9^x}

Should I plot the individual lines as individual addplot?

Thanks.

2
  • Is it a particlar point or just eyeballing?
    – percusse
    Commented Sep 12, 2014 at 11:54
  • Well, I'd prefer a particular point, but eyeballing would work too! Commented Sep 12, 2014 at 12:59

2 Answers 2

4

A PSTricks solution:

\documentclass{article}

\usepackage{pstricks-add}

\def\interpolation[#1](#2,#3)#4{
  \psline[linestyle = #1](0,#3)(#2,#3)(#2,0)
  \uput[270](#2,0){$x_{#4}$}
  \uput[180](0,#3){$y_{#4}$}
}

\begin{document}

\psset{xunit = 2, yunit = 1.5}
\begin{pspicture}(-0.24,-0.24)(4.68,5.76)
\def\xA{0.4}
\def\yA{1.2}
\def\xB{1.6}
\def\yB{1.6}
\def\xC{2.1}
\def\yC{2.5}
\def\xD{2.8}
\def\yD{3.7}
\def\xE{3.8}
\def\yE{4.7}
  \psaxes[
    labels = none,
    xticksize = -0.14 5,
    yticksize = -0.105 4,
    tickwidth = 0.5pt,
    tickcolor = gray!50
  ]{->}(0,0)(4.5,5.5)[$x$,0][$y$,90]
  \psset{
    dotsize = 3pt 2,
    dotstyle = Bo
  }
  \psline[
    linewidth = 1.2pt,
    linecolor = blue,
    showpoints = true,
  ](\xA,\yA)(\xB,\yB)(\xC,\yC)(\xD,\yD)(\xE,\yE)
  \psdot[
    fillcolor = red
  ](\xC,\yC)
  \interpolation[dotted](\xB,\yB){1}
  \interpolation[dashed](\xC,\yC){}
  \interpolation[dotted](\xD,\yD){2}
\end{pspicture}

\end{document}

output

Notice that all you have to do is choose the coordinates for the points, i.e., the values of \xA,...,\xE and \yA,...,\yE, and the drawing will adjust accordingly.

Update

Notice that had it not been for the labels on the axes, we could have defined the points using \pnodes instead of all the \defs. (There may be a smart way of defining the coordinates for the points but I don't know of it.)

2
  • Do you think I'd somehow be able to implement this with my current axis environment with pgfplots? Commented Sep 12, 2014 at 12:58
  • @SheheryarZaidi I don't know PGFPlots so I cannot help you with that---sorry! Commented Sep 12, 2014 at 13:03
1

It is quite easy to achieve what you want. Please have a look at the comments in the code.

% used PGFPlots v1.16
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        % only show ticks at the data point ...
        xtick=data,                 ytick=data,
        % ... with numbered labels
        xticklabel={$x_{\ticknum}$},yticklabel={$y_{\ticknum}$},
        % change `clip mode` thus nodes of TikZ stuff outside the axis area
        % is not clipped away (i.e. the $x$ and $y$ label)
        clip mode=individual,
    ]
        % plotting points with connecting lines is PGFPlots default,
        % thus the easiest way is to provide the data as a table
        % (either inline as here or as data file)
        \addplot table {
            x   y
            0.5 1
            1.5 1.5
            3   4
            4   5
        }
            % now you can specify a point where you want to add a "interpolated point"
            coordinate [pos=0.5] (A)
        ;

        % draw the horizontal and vertical lines to the axis start points
        \draw [dashed] ({rel axis cs:0,0} |- A)
            % including the labels
            node [left] {$y$}
                -| ({rel axis cs:0,0} -| A)
            node [below] {$x$};
        % draw a circle at the "interpolated point" with the same marker size as before
        % (2pt is the default/initial value)
        \filldraw [red] (A) circle (2pt);
    \end{axis}
\end{tikzpicture}
\end{document}

image showing the result of above code

1
  • Would there be a way to instead of specifying the percentage where the point A would be placed, to instead type te exact x of A then have it interpolate into the y value?
    – Felipe9
    Commented Jul 19 at 16:44

You must log in to answer this question.

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