4

I have plot of y = 2 log_{2}(x), or equivalently, a plot of y = (2/ln(2))ln(x), a tangent line at (4,4), and a secant line through (4,4) and (16,8). I draw two "pins" to label these lines. The slopes of each are perpendicular to their respective lines. I would like them to be the same length - without a tedious calculation. How can I make them both the same length - maybe 0.75cm long?

I suspect that drawing this using the canvas coordinate system would be the most efficient. I have also seen an option for pin in a node command in pgfplots, and mathtools can be used to give me the "pins" perpendicular to the given lines.

\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}
  • 3
    Possible duplicate of pgfplot pin length – Alenanno Aug 27 '16 at 20:47
  • By the way, you can do pins using simple \node commands. addplot is unnecessarily complicated for this, I think. – Alenanno Aug 27 '16 at 20:48
  • @Alenanno I looked at the link that you provided. I am not sure that using the optionpin in a node command will give me what I want - a "pin" perpendicular to the given lines. – user74973 Aug 27 '16 at 21:19
  • @Alenanno Do you want to give the code for drawing a line from the canvas coordinate system? – user74973 Aug 31 '16 at 15:59
  • Not sure what you're asking about. Can you clarify your question? – Alenanno Aug 31 '16 at 16:01
6
+50

You can add coordinates to plots. From there it's easy to draw custom lines with nodes, even outside of the axis environment.

Output

enter image description here

Code

\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}
| improve this answer | |
  • This is exactly the code that I want to use! (I like the draw=gray option, too.) – user74973 Sep 4 '16 at 11:37
  • @user74973 Glad to help. That option is draw=gray because if you wrote gray, the line would still be just gray, but then the node contents would be affected too. – Alenanno Sep 4 '16 at 11:41
  • Please make the following edits to make the appearance nicer. I already made these edits on my file. These modifications are for the members of this website. Use the optionfont=\footnotesize both both nodes, anchor=west for the node for the tangent line, and anchor=east for node for the secant line. (I will delete this comment.) – user74973 Sep 4 '16 at 15:11
  • @Adelyn both nodes do use font=\footnotesize, see the style above. I'll edit the answer as soon as I can, even though I don't understand why this is so important for you. :P – Alenanno Sep 5 '16 at 16:16
  • 1
    @user74973 Fixed. – Alenanno Sep 18 '16 at 17:48

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.