How to Make a Vertical Box Plot Horizontal in PGFPlots
At a glance
- 1. Set the direction
Add boxplot/draw direction=x to the axis options or to the boxplot plot options.
- 2. Move categories
For horizontal boxes, put category positions and labels on the y-axis.
- 3. Check the values
Use the x-axis for the numeric boxplot scale, including whiskers, quartiles, and median.
To turn a vertical PGFPlots box plot into a horizontal one, set its draw direction to x. The numeric values then run along the x-axis, while the box plot’s position—and any category label—runs along the y-axis.
The example below draws one horizontal box plot from prepared summary statistics. It is useful both as a complete starting point and as a reference for changing an existing plot.
Complete example
The code requires PGFPlots and its statistics library. Save it as a .tex file and compile with a LaTeX engine such as pdfLaTeX.
\documentclass{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{statistics}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
boxplot/draw direction=x,
xmin=0, xmax=10,
ymin=0.5, ymax=1.5,
ytick={1},
yticklabels={Group A},
xlabel={Value},
xmajorgrids,
]
\addplot+[
boxplot prepared={
lower whisker=1,
lower quartile=3,
median=5,
upper quartile=6,
upper whisker=9,
},
boxplot/draw position=1,
fill=blue!20,
] coordinates {};
\end{axis}
\end{tikzpicture}
\end{document}
Here, the whiskers extend from 1 to 9, the box spans the lower and upper quartiles from 3 to 6, and the median is at 5. The box is placed at y-position 1, where the y-axis tick is labelled “Group A.” The horizontal direction key makes those statistical values x-coordinates.
Changing an existing vertical plot
Find the axis options or the options of the box-plot \addplot and add:
boxplot/draw direction=x,
For example, you can set the direction at the axis level:
\begin{axis}[
boxplot/draw direction=x,
xlabel={Value},
ylabel={Group},
]
Then adjust the axis labels and ticks to match the new layout. Category ticks that were on the x-axis for a vertical plot generally belong on the y-axis for a horizontal one. The numerical scale belongs on the x-axis.
If your code supplies the boxplot’s statistics directly with boxplot prepared, those statistics now describe horizontal positions. For box plots computed from a data table, check how the data column is selected in your plot: the values used for the box must be supplied in the direction expected by the horizontal plot. Do not assume that changing only the draw-direction key also changes a table’s x or y column selector.
What the position setting does
A box plot has two different coordinates: the numeric coordinate of each statistic and the position of the box within the plot. With a horizontal box plot, the first is along x and the second is along y. In the example, boxplot/draw position=1 places the box at y = 1, matching ytick={1}.
For several groups, give each box a different draw position and add corresponding y-axis ticks and labels. For example, positions 1, 2, and 3 can represent three groups. Keep enough space in the y-axis limits for the boxes to be visible.
Common mistakes
- Swapping axis labels without changing the plot direction: This changes the text, not the geometry. Set
boxplot/draw direction=xto make the box horizontal. - Leaving category labels on the x-axis: Horizontal box plots are positioned across y, so group labels usually need to be y ticks.
- Confusing the box position with the data values:
boxplot/draw positioncontrols where the box sits; whisker and quartile values control its extent along the numeric axis. - Using an incompatible axis range: A box with draw position 1 will not appear if the visible y-range excludes 1. Likewise, the x-range must include the whiskers and any outliers.
- Changing a table selector blindly: If a plot reads values from a table, verify the selected coordinate and the library’s expected input direction rather than merely relabelling the axes.
References
- PGFPlots Manual: Statistics Plots
Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.