How to Change the Anchor of a TikZ or PGFPlots Pin
At a glance
- 1. Choose the pin
Use the pin option on the node placed at the point you want to annotate.
- 2. Set its anchor
Put anchor=... inside the pin’s local options, before the angle and label.
- 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 direction | Typical label anchor | Text extends |
|---|---|---|
| Right, 0° | west | Right |
| Above, 90° | south | Up |
| Left, 180° | east | Left |
| Below, 270° | north | Down |
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 afterpin=, or inevery pin. - Putting the options outside the pin. Use
pin={[anchor=west]0:{Label}}, notpin=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=eastusually makes the text extend left.anchor=westmay 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
- The TikZ and PGF Packages: Manual, section on Nodes and Edges (Pins)
- The PGFPlots Package: Manual
Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.