How to Add a Label to a Plot in PGFPlots
At a glance
- 1. Choose label type
Use a legend entry to identify a plotted curve, or place a node in the axis to annotate a particular location.
- 2. Add the label
Put the legend entry after its plot, or position a node using an axis coordinate.
- 3. Compile and adjust
Check the label’s placement and styling; move or anchor an annotation to prevent overlap.
In PGFPlots, “adding a label to a plot” can mean either naming a curve in a legend or writing text at a particular place inside the graph. The first is usually what you want when distinguishing several curves; the second is useful for calling out a point, region, or feature.
The examples below show both approaches in a complete document. They use the pgfplots package and an axis environment.
Label a curve in the legend
Place \addlegendentry{...} immediately after the \addplot whose curve it names. PGFPlots then adds that text and the plot’s line or marker style to the legend.
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xlabel={$x$},
ylabel={$y$},
legend pos=north west
]
\addplot[blue, thick, domain=-2:2, samples=100] {x^2};
\addlegendentry{$y=x^2$}
\addplot[red, dashed, thick, domain=-2:2, samples=100] {2*x};
\addlegendentry{$y=2x$}
\end{axis}
\end{tikzpicture}
\end{document}
Each legend entry belongs to the plot command immediately before it. If you swap the order of entries, the text can describe the wrong curve. The legend pos option controls where the legend is placed; other positions include north east, south west, and outer north east.
Put text directly on the graph
For a label beside a point or feature, use a TikZ node inside the axis environment. The coordinate form (axis cs:x,y) tells PGFPlots to interpret the values in the graph’s data coordinates, rather than in centimeters or another drawing unit.
For example, add this inside the axis, after the plot commands:
\node[above right] at (axis cs:1,1) {$y=x^2$};
At , the curve has , so (axis cs:1,1) is a point on that curve. The above right option places the text above and to the right of the coordinate. You can use anchors or positioning options such as above, below, left, and right to adjust it.
Here is a compact complete example using a direct annotation instead of a legend:
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xlabel={$x$},
ylabel={$y$},
domain=0:3,
ymin=0,
ymax=10
]
\addplot[blue, thick, samples=100] {x^2};
\node[above right] at (axis cs:2,4) {$y=x^2$};
\end{axis}
\end{tikzpicture}
\end{document}
The node is placed at , which lies on the plotted parabola. If a label should point to a feature, you can also use a pin option, or draw an arrow from a node to the point.
Which kind of label should you use?
| Goal | Use |
|---|---|
| Identify one curve among several | \addlegendentry{...} after its \addplot |
| Name a curve directly on the graph | A node positioned with (axis cs:x,y) |
| Label an axis or the whole figure | Axis options such as xlabel, ylabel, or a figure caption |
| Reference the figure elsewhere in the document | LaTeX \label and \ref on the figure environment |
A LaTeX \label command does not write visible text beside a curve. It creates a reference target for commands such as \ref; use a legend entry or node when you want visible plot text.
Common mistakes
- Putting the legend entry before the plot. Associate each
\addlegendentrywith the\addplotimmediately before it. - Using the wrong coordinate system. Inside an axis, use
(axis cs:x,y)for data coordinates. A plain TikZ coordinate such as(2,4)is not necessarily interpreted as the graph point . - Choosing a point outside the visible range. A node can be clipped or hidden if its coordinate lies outside the axis limits. Check
xmin,xmax,ymin, andymaxif it does not appear. - Expecting
\labelto print text. It is for cross-referencing, not for annotating a plotted line. - Letting the text cover important data. Move the node with a different placement option or choose a nearby coordinate. For multiple curves, a legend is often clearer than several labels on the graph.
References
Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.