4

Please note that I am not using the pin option of a node as in How can I force TikZ pin angle? and TikZ node pin angle in 2D plot, but am instead using the pin in data option in a TikZ data visualization.

I have a curve with a pinned label:

Plot of y = f(x), labeled

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{datavisualization}
\usetikzlibrary{datavisualization.formats.functions}
\begin{document}
\tikz[scale=4]
    \datavisualization[
        school book axes,
        visualize as smooth line=myfunction,
        myfunction={pin in data={text=$f(x)$, when=x is 0.5, pin angle=90}}]
    data [set=myfunction, format=function] {
        var x : interval [0:1];
        func y = \value x^2;
    };
\end{document}  

As might be inferred from my setting pin angle to 90, I want the pin to be precisely vertical. I learned from the TikZ manual (§ 61.8.2) that what this tag actually does (along with pin length) is introduce an additional offset of the label, after the label has already been offset by whatever algorithm label in data uses. As a consequence, we are adding the diagonal vector from label in data to my vertical (pin angle=90) vector, and we do not get a vertical resultant vector.

I want my pin to be vertical. How can I make it behave?

1

The position of the label is calculated through the auto option. It is designed to make labels floating alongside the curve without overlapping. As a result the pin-edge is nearly orthogonal to the tangent line for safety.

It is not difficult to override auto but it may ruin the whole engine. A workaround is to use the temporary point (label visualizer coordinate) to draw your own pin. This point locates at the datapoint which the latest label refers to.

(Nevertheless you still have some options such as rewriting pin in data/label in data. See tikzlibrarydatavisualization.code.tex for their original definition.)

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{datavisualization}
\usetikzlibrary{datavisualization.formats.functions}
\begin{document}
\tikz[scale=4]{
    \datavisualization[
        school book axes,
        visualize as smooth line=myfunction,
        myfunction={label in data={text=, when=x is 0.5, pin angle=90}}]
    data [set=myfunction, format=function] {
        var x : interval [0:1];
        func y = \value x^2;
    };
    \draw[red](label visualizer coordinate)--+(0,.1)node[above]{$f(x)$};}
\end{document}
  • This looks like what I need. I just have one more question: if I had two functions in the visualization, and each were labeled, would it be possible to adapt your code to modify the positions of both? – Radon Rosborough Jul 6 '15 at 13:44

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.