How to Control the Layout of Bar Charts in LaTeX
At a glance
- 1. Choose a layout
Use grouped bars to compare series, stacked bars to show totals, or separate axes to compare distinct charts.
- 2. Set categories and spacing
Define category positions, tick labels, bar width, axis dimensions, and the legend.
- 3. Arrange multiple charts
Use PGFPlots groupplots to place axes in rows or columns with consistent sizing.
PGFPlots can lay out bar charts in several useful ways: bars can sit beside one another for comparisons, stack to show a total, or appear in separate axes arranged as a panel. The right choice depends on whether you are comparing values within each category or comparing distinct charts.
The example below creates two small bar charts side by side. Each compares two series across three categories, and the groupplots library controls the panel arrangement.
Choose the bar-chart layout
Use ybar for a conventional vertical bar chart. When you add multiple data series, PGFPlots offsets their bars within each category, producing a grouped or clustered layout. This makes it easy to compare series directly.
Use ybar stacked when the series represent parts of a whole. The bars are stacked, so their combined height shows the total for each category. A stacked chart is less suited to comparing individual values that do not share a common baseline.
For separate charts, create separate axis environments. If you want a regular grid of axes, load the groupplots library and use a groupplot environment. This keeps panel sizes and spacing consistent.
Example: two charts side by side
This complete document uses PGFPlots and its groupplots library. Each panel contains grouped bars for two series.
\documentclass{article}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{groupplot}[
group style={group size=2 by 1, horizontal sep=1.5cm},
width=6cm,
height=5cm,
ybar,
bar width=8pt,
symbolic x coords={a,b,c},
xtick=data,
xticklabels={Q1,Q2,Q3},
ymin=0,
ylabel={Value},
legend pos=north west,
]
\nextgroupplot[title={North}, ymax=12]
\addplot coordinates {(a,7) (b,9) (c,6)};
\addplot coordinates {(a,5) (b,4) (c,8)};
\legend{Plan,Actual}
\nextgroupplot[title={South}, ymax=12]
\addplot coordinates {(a,6) (b,8) (c,7)};
\addplot coordinates {(a,4) (b,6) (c,9)};
\legend{Plan,Actual}
\end{groupplot}
\end{tikzpicture}
\end{document}
The group size=2 by 1 setting means two columns and one row. Change it to 1 by 2 to stack the charts vertically. horizontal sep adjusts the gap between side-by-side panels; for a vertical arrangement, adjust vertical sep instead.
Within each panel, symbolic x coords declares the category keys used in the coordinates, and xticklabels supplies the displayed labels. The two \addplot commands produce two bars per category. Adjust bar width, width, and height to change the bars and overall panel proportions. The ymax setting provides headroom above the tallest bar.
Stacking bars instead
To show how each category’s total is divided between series, replace ybar with ybar stacked. For example, the two values for category a in the North panel would stack to a total of 12. Choose a suitable ymax for the sum of the series, rather than the largest individual value.
You can also use one axis instead of a groupplot if you need only one chart. The same ybar, category, and bar-width options apply.
Common layout mistakes
- Using the wrong layout for the comparison. Grouped bars are useful when you need to compare series within each category. Stacked bars emphasize totals and can make comparisons between the upper segments harder.
- Forgetting to match category keys. With
symbolic x coords, the keys in every\addplot coordinateslist must match the declared keys. A spelling mismatch can cause an error or an unexpected category. - Making bars or panels too wide. A large
bar widthcan crowd adjacent bars; overly wide panels can also waste space in a document. Adjustbar widthand the axis dimensions together, then check the chart at its intended printed size. - Placing legends where they cover data. The example puts each legend inside the upper-left corner. If that area contains bars, choose another legend position or place the legend outside the axis and allow enough room in the surrounding layout.
- Giving panels incomparable scales by accident. Different
ymaxvalues can make equally tall bars represent different values. Use the same limits when visual comparison between panels matters; use different limits only when that difference is intentional and clearly communicated.
References
Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.