How to Align Histogram x-Ticks to the Left of Each Bin in PGFPlots
At a glance
- 1. Use interval bars
Choose ybar interval so each plotted value fills the interval from its x-coordinate to the next one.
- 2. List bin boundaries
Provide one more x-coordinate than the number of bins; the final point closes the last interval.
- 3. Set tick positions
Set xtick to the left boundary of each bin where you want a tick.
- 4. Add labels as needed
Use xticklabels to control the displayed text independently of the tick positions.
In a PGFPlots histogram, you can put x-axis ticks at the left edge of each bin by choosing the tick coordinates explicitly. The usual approach is ybar interval: each data point supplies a bin height, and the interval from that point’s x-coordinate to the next point’s x-coordinate supplies the bin’s horizontal span.
A complete example
This example draws four adjacent bins over the intervals , , , and . The x ticks are placed at the left boundary of each bin.
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
ybar interval,
xmin=0, xmax=4,
ymin=0,
xtick={0,1,2,3},
xticklabels={0,1,2,3},
xlabel={Left boundary of bin},
ylabel={Count},
]
\addplot coordinates {
(0,3) (1,5) (2,2) (3,4) (4,0)
};
\end{axis}
\end{tikzpicture}
\end{document}
The first four coordinates give bin heights 3, 5, 2, and 4. The final coordinate, (4,0), provides the right boundary of the last bin; it does not add a fifth bin. Since xtick lists 0 through 3, every tick is at the left edge of one of the four bars. Add 4 to xtick if you also want a tick at the final, rightmost boundary.
How the interval style works
With ybar interval, consecutive x-coordinates define the width of each bar. In the example, the point at x = 1 begins a bin that extends to x = 2, and its y-value gives that bin’s height. This is useful when your histogram bins have known boundaries or different widths.
You need one more x-coordinate than the number of bins. For bins, provide boundary coordinates. The last coordinate closes the last interval. Set xtick separately: PGFPlots does not require ticks to appear at every coordinate, nor must the tick labels be the coordinate values.
If your data are listed in a table, the same idea applies: use the bin-boundary column as x-coordinates, provide the terminal boundary, and set xtick to the left boundaries you want to show.
Use labels that are not the boundary values
You can keep the tick positions at the left edges while displaying different text. For example, to label the four bins A through D, change the axis options to:
xtick={0,1,2,3},
xticklabels={A,B,C,D},
The first label is printed at x = 0, the second at x = 1, and so on. This separates the tick’s numeric location from its printed label.
For labels centered under each bin instead, use the bin midpoints as tick positions. For unit-width bins from 0 to 4, those positions are 0.5, 1.5, 2.5, and 3.5:
xtick={0.5,1.5,2.5,3.5},
Common mistakes
- Leaving out the final boundary: Four bins require five x-coordinates. Without the last coordinate, the final interval cannot be drawn as intended.
- Using
xtick=datawithout checking the result: This can place ticks at all supplied coordinates, including the closing boundary. An explicitxticklist gives you precise control. - Confusing left-edge ticks with centered labels: Left-edge ticks use bin boundaries; labels centered under bars use bin midpoints. Choose based on what the axis is meant to communicate.
- Treating
ybar intervalas automatic histogram computation: It draws bars from supplied coordinates; it does not calculate bin counts from raw observations.
If by “ticks to the left” you mean that tick marks or labels should appear along the left side of the plot, that is a y-axis placement issue. The x-axis is horizontal; use options such as axis y line=left and yticklabel pos=left for the y-axis. For ticks aligned with left bin boundaries, use the xtick approach shown above.
References
Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.