Tx · LaTeX & TikZ

How to Change the Anchor of a TikZ or PGFPlots Pin

Published By the 9to5Science editorial team

At a glance

  1. 1. Choose the pin

    Use the pin option on the node placed at the point you want to annotate.

  2. 2. Set its anchor

    Put anchor=... inside the pin’s local options, before the angle and label.

  3. 3. Check the direction

    Choose an anchor that faces the plotted point so the label extends in the intended direction.

A TikZ pin is a small label node attached to another node or coordinate by a pin edge. To change how the label is aligned, set the anchor on the pin label node. The point being annotated is a separate node, so changing that node’s anchor usually does not change the pin text alignment.

Set the anchor for one pin

Add the label-node options in square brackets immediately after pin=. The general form is:

pin={[<label-node options>]<angle>:{<label text>}}

For example, pin={[anchor=west]0:{Peak}} places the pin at angle 0 and uses the label’s west anchor. Because the west edge is attached at the pin position, the text extends to the right.

Here is a complete example using PGFPlots:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}

\begin{document}

\begin{tikzpicture}
  \begin{axis}[
    xmin=0, xmax=2,
    ymin=0, ymax=2,
    xlabel={$x$}, ylabel={$y$}
  ]
    \addplot[only marks, mark=*] coordinates {(1,1)};
    \node[pin={[anchor=west]0:{Peak}}]
      at (axis cs:1,1) {};
  \end{axis}
\end{tikzpicture}

\end{document}

The axis cs:1,1 coordinate identifies the plotted point in axis coordinates. The node at that point has a pin at angle 0, pointing to the right. The local anchor=west option aligns the left side of the label at the pin position. You can add other label-node options there too, such as font=\small or align=left.

Choose an anchor that matches the direction

The anchor tells TikZ which point on the label node to place at the pin position. A useful starting point is to have the anchor face the annotated point, so the text extends away from it:

Pin directionTypical label anchorText extends
Right, 0°westRight
Above, 90°southUp
Left, 180°eastLeft
Below, 270°northDown

For instance, to put a label to the left of a point, use pin={[anchor=east]180:{Left label}}. These angles describe the pin direction in the TikZ picture; they are not the same thing as choosing a label anchor. If the label overlaps the plot or the pin edge looks awkward, adjust the angle, the anchor, or the pin distance.

Apply an anchor to every pin

When you want a consistent anchor for all pins in a picture, define the every pin style rather than repeating the option:

\begin{tikzpicture}[
  every pin/.style={anchor=west, font=\small}
]
  \node[pin=0:{First label}, pin=0:{Second label}] at (0,0) {};
\end{tikzpicture}

For pins inside a PGFPlots axis, set the style on the enclosing tikzpicture so it applies to the TikZ pin nodes:

\begin{tikzpicture}[
  every pin/.style={anchor=west, font=\small}
]
  \begin{axis}[
    xmin=0, xmax=2,
    ymin=0, ymax=2
  ]
    % plots and nodes
  \end{axis}
\end{tikzpicture}

Alternatively, define the TikZ style with \tikzset{every pin/.style={anchor=west, font=\small}}. Use a per-pin option when only one label needs different alignment; use every pin when the rule should be shared. A local option can make the intent clearer even when the default style is defined elsewhere.

Common mistakes

  • Setting the anchor on the point node. The pin label is its own node. Put anchor=... inside the brackets after pin=, or in every pin.
  • Putting the options outside the pin. Use pin={[anchor=west]0:{Label}}, not pin=0:{Label}, anchor=west; the latter sets an option on the main node rather than specifically on the pin label.
  • Choosing an anchor that points the wrong way. For a pin pointing left, anchor=east usually makes the text extend left. anchor=west may instead place it across the point or edge.
  • Expecting the anchor to move the pin’s attachment point. The anchor aligns the label box. To change its location relative to the annotated point, change the pin angle or distance instead.

The key distinction is simple: use the pin’s local options to control the label node, and adjust the pin angle or distance to control where that label sits.

References

  1. The TikZ and PGF Packages: Manual, section on Nodes and Edges (Pins)
  2. The PGFPlots Package: Manual

Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.