pgfplots 插值

pgfplots 插值

插入图形的最佳方法是什么,并像这样显示插值(使用 pgfplots): 插值
(来源:blueleafsoftware.com

我在环境中绘制了以下代码axis

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

我应该将各条线条绘制为单独的addplot吗?

谢谢。

答案1

PSTricks 解决方案:

\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}

输出

\xA请注意,您所要做的就是选择点的坐标,即、...、\xE\yA、...、的值\yE,然后绘图将相应调整。

更新

请注意,如果没有轴上的标签,我们可以使用\pnodes而不是所有\defs 来定义点。(可能有一种定义点坐标的聪明方法,但我不知道。)

答案2

实现你想要的非常容易。请查看代码中的注释。

% 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}

该图显示了上述代码的结果

相关内容