How to Use xtick, xticklabels, and extra x ticks in PGFPlots
At a glance
- 1. Set regular tick positions
Use xtick to specify where the ordinary x-axis ticks appear.
- 2. Assign regular labels
Use xticklabels to provide one label for each position in xtick.
- 3. Add extra positions
Use extra x ticks for additional positions without replacing the ordinary ticks.
- 4. Label extra ticks
Use extra x tick labels to label those additional positions.
PGFPlots lets you choose both where x-axis ticks appear and what text is printed beside them. The key distinction is between ordinary ticks and extra ticks: xtick and xticklabels control the first set, while extra x ticks and extra x tick labels control an additional set.
Ordinary ticks: xtick and xticklabels
Set xtick to a comma-separated list of numeric positions. Set xticklabels to the labels you want shown at those positions, in the same order.
xtick={0,1,2,3},
xticklabels={zero,one,two,three},
This places ordinary ticks at and labels them “zero,” “one,” “two,” and “three.” If you omit xtick, PGFPlots normally chooses tick positions automatically. If you set xtick but omit xticklabels, PGFPlots generates labels for the specified positions using its normal number formatting.
The two lists should correspond position by position. For example, the first label is associated with the first tick position, not with a position inferred from its text.
Add ticks with extra x ticks
Use extra x ticks when you want to mark additional positions while keeping the ordinary ticks. The matching labels go in extra x tick labels:
extra x ticks={1.5,2.5},
extra x tick labels={midpoint,between},
Here, “midpoint” appears at and “between” at . These options do not replace xtick or xticklabels; both sets can appear on the same axis. Extra ticks also do not define the axis limits. Set limits separately with options such as xmin and xmax when you need specific bounds.
Complete working example
This example shows four ordinary ticks with custom labels and two additional ticks with their own labels. It uses the pgfplots package and the standalone document class.
\documentclass[tikz,border=3pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xmin=0, xmax=3,
xtick={0,1,2,3},
xticklabels={zero,one,two,three},
extra x ticks={1.5,2.5},
extra x tick labels={midpoint,between},
]
\addplot+[mark=*] coordinates {
(0,1) (1,2) (2,1.5) (3,2.5)
};
\end{axis}
\end{tikzpicture}
\end{document}
The regular labels remain attached to . The extra labels are attached to and . Both groups are specified in increasing position order, so the corresponding lists are easy to check. The axis limits are set explicitly to keep the displayed range from to .
Hiding ordinary ticks while keeping extra ticks
If you want only the extra ticks, suppress the ordinary tick list with xtick=\empty and specify the extra positions and labels as usual:
xtick=\empty,
extra x ticks={1,2},
extra x tick labels={first,second},
This is different from setting xticklabels=\empty: the latter removes the ordinary tick labels, but does not by itself remove the ordinary tick marks. Choose the option that matches whether you want to hide the marks, their labels, or both.
Common mistakes
- Using the wrong label key.
xticklabelslabels ordinary ticks; it does not label positions listed underextra x ticks. Useextra x tick labelsfor those. - Mismatching the lists. Keep the label entries in the same order as their tick positions. A label list with the wrong number of entries can leave the intended mapping unclear or produce unexpected labels.
- Expecting extra ticks to replace regular ones. They are an additional set. If you do not want ordinary ticks, suppress them explicitly.
- Expecting a tick option to expand the axis. Tick positions and axis limits are separate settings. Make sure the desired positions fall within the visible range set by the axis limits.
- Confusing positions with text. A tick position is numeric, such as
1.5; its displayed label can be arbitrary text, such asmidpoint.
For y-axis ticks, the corresponding keys are ytick, yticklabels, extra y ticks, and extra y tick labels.
References
Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.