在 PGFPlots 中绘制垂直于(图的)且具有一定长度的“图钉”

在 PGFPlots 中绘制垂直于(图的)且具有一定长度的“图钉”

我有 的图y = 2 log_{2}(x),或者说 的图,一条y = (2/ln(2))ln(x)在 处的切线,以及一条通过和 的(4,4)割线。我画了两个“图钉”来标记这些线。每个图钉的斜率都垂直于它们各自的线。我希望它们的长度相同——而不需要繁琐的计算。我怎样才能使它们的长度相同——也许更长?(4,4)(16,8)0.75cm

我怀疑使用画布坐标系绘制这个是最有效的。我还在pin中的节点命令中看到了 选项pgfplots,并且mathtools可以用来给我垂直于给定线的“引脚”。

\documentclass{amsart}
\usepackage{tikz}
    \usetikzlibrary{calc,intersections}
\usepackage{pgfplots}
    \pgfplotsset{compat=1.11}
\usepackage{mathtools,array}

\begin{document}
\begin{tikzpicture}
\begin{axis}[width=5in, height=2.5in, clip=false,
    axis lines=middle,
    xmin=-4,xmax=32,
    ymin=-6,ymax=16,
    restrict y to domain=-6:16,
    xtick={4,8.6562,16}, ytick={\empty},
    ticklabel style={font=\tiny,fill=white},
    xticklabels={$a$, $c$, $x_{\circ}$},
    xlabel=$x$,ylabel=$y$,
    axis line style={latex-latex},
    axis line style={shorten >=-12.5pt, shorten <=-12.5pt, latex-latex},
    xlabel style={at={(ticklabel* cs:1)},  xshift=12.5pt, anchor=north west},
    ylabel style={at={(ticklabel* cs:1)},  yshift=12.5pt, anchor=south west}
]

\addplot[samples=501, name path=a_logarithm_function, domain=1/2^6:32]{2*log2(x)} node[right, pos=1, font=\footnotesize]{\makebox[0pt][l]{$y = f(x)$}};

%A secant line segment is drawn between (4,4) and (16,8).
\addplot[samples=2, dashed, domain=4:16] {(1/3)*x + 8/3};

%A tangent line at (4,4) is drawn; the slope of it is 1/(2 ln(2)).
\addplot[samples=2, latex-latex, domain=-4:{4+24*ln(2)}] {(1/(2*ln(2)))*x - 2/ln(2) + 4};

\addplot[samples=2, latex-latex, domain=-4:32] {(1/3)*x - 2/ln(2) + (2/ln(2))*ln(6/ln(2))};


%A "pin" is drawn to the tangent line at (a, f(a)). It is to be
%perpendicular to it; so, its slope is -2ln(2) = 2ln(1/2).
\addplot[samples=2, domain={16-0.15}:{16-2/3}] {2*ln(1/2)*x + 32*ln(2) + 6/ln(2) + 4} node[anchor=east, pos=1, font=\footnotesize]{$y=f(a)+f^{\prime}(a)(x-a)$};

%A "pin" is drawn to the secant line. It is to be perpendicular to the %secant line; so, its slope is -3.
\addplot[samples=2, domain={10+0.15}:{10+2/3}] {-3*x + 36} node[right, pos=1, font=\footnotesize]{\makebox[0pt][l]{$y=f(a)+f^{\prime}(c)(x-a)$}};


\draw [fill] (4,4) circle [radius=1.5pt];
\draw [fill] ({6/ln(2)}, {(2/ln(2))*ln(6/ln(2))}) circle [radius=1.5pt];

\end{axis}
\end{tikzpicture}
\end{document}

答案1

您可以向图中添加坐标。从那里,即使在环境之外,也可以轻松地用节点绘制自定义线条axis

输出

在此处输入图片描述

代码

\documentclass[margin=20pt]{standalone}
\usepackage{mathtools,array}
\usepackage{tikz}
\usepackage{pgfplots}

\usetikzlibrary{calc,intersections}

\pgfplotsset{compat=1.11}

\tikzset{
    mypin/.style={draw=gray, shorten <=1mm, shorten >=1mm},
    nod/.style={font=\footnotesize, inner sep=1mm, outer sep=-1mm}
}

\begin{document}
\begin{tikzpicture}
\begin{axis}[width=5in, height=2.5in, clip=false,
    axis lines=middle,
    xmin=-4,xmax=32,
    ymin=-6,ymax=16,
    restrict y to domain=-6:16,
    xtick={4,8.6562,16}, ytick={\empty},
    ticklabel style={font=\tiny,fill=white},
    xticklabels={$a$, $c$, $x_{\circ}$},
    xlabel=$x$,ylabel=$y$,
    axis line style={latex-latex},
    axis line style={shorten >=-12.5pt, shorten <=-12.5pt, latex-latex},
    xlabel style={at={(ticklabel* cs:1)},  xshift=12.5pt, anchor=north west},
    ylabel style={at={(ticklabel* cs:1)},  yshift=12.5pt, anchor=south west}
]

\addplot[samples=501, name path=a_logarithm_function, domain=1/2^6:32]{2*log2(x)} node[right, pos=1, font=\footnotesize]{\makebox[0pt][l]{$y = f(x)$}};
\addplot[samples=2, latex-latex, domain=-4:32] {(1/3)*x - 2/ln(2) + (2/ln(2))*ln(6/ln(2))};

\addplot[samples=2, dashed, domain=4:16] {(1/3)*x + 8/3} 
    coordinate[midway] (a') coordinate (b');
\addplot[samples=2, latex-latex, domain=-4:{4+24*ln(2)}] {(1/(2*ln(2)))*x - 2/ln(2) + 4} 
    coordinate[pos=.7] (a) coordinate (b);

\draw [fill] (4,4) circle [radius=1.5pt];
\draw [fill] ({6/ln(2)}, {(2/ln(2))*ln(6/ln(2))}) circle [radius=1.5pt];

\end{axis}
\draw[mypin] (a) -- ($(a)!.75cm!90:(b)$) node[nod, anchor=south east] {$y=f(a)+f^{\prime}(a)(x-a)$};
\draw[mypin] (a') -- ($(a')!.75cm!-90:(b')$) node[nod, anchor=north west] {$y=f(a)+f^{\prime}(c)(x-a)$};
\end{tikzpicture}
\end{document}

相关内容