# Grouped bar chart in pgfplots, explained

> How a grouped bar chart is drawn with pgfplots: ybar, bar width, coordinates, a legend and year labels without commas, plus two variations.

URL: https://www.labcanvas.com/tikz/examples/plots-and-charts/grouped-bar-chart

Updated 2026-11-22. Original figure: Grouped bar chart, © Packt (code from LaTeX Graphics with TikZ by Stefan Kottwitz), MIT licence (licence text: https://www.labcanvas.com/tikz-library/credits.html#packt), from https://github.com/PacktPublishing/LaTeX-graphics-with-TikZ/blob/82e099639f46ee92bc622a01c739e7f7a3e87f4b/14-diagrams/13-bar-chart.tex. Walkthrough and variations by LabCanvas; the variation code is CC0 1.0 (https://creativecommons.org/publicdomain/zero/1.0/). Pictures typeset with TeX Live 2026 and PGF 3.1.12. Category: [TikZ and pgfplots chart examples](https://www.labcanvas.com/tikz/examples/plots-and-charts).

## Quick answer

This chart compares three keywords, tikz, pstricks and metapost, year by year from 2007 to 2013, as groups of three thin bars. It is one pgfplots `axis` with the `ybar` option, which turns every `\addplot` into bars and sets them side by side, three `\addplot coordinates` commands holding the data, and a `\legend`. The one trick is in the x tick labels: the years are numbers to pgfplots, so the thousands separator is switched off to print 2007 rather than 2,007.

## The figure and its code

![A grouped bar chart titled Keyword popularity in Google trends: for each year from 2007 to 2013 three thin bars, blue for tikz rising from 16 to 55, red for pstricks falling from 39 to 8 and brown for metapost falling from 22 to 4, with a legend in the top left corner.](https://www.labcanvas.com/tikz-snippets/fig-grouped-bar-chart/original.webp)

Copyright (c) 2022 Packt, MIT licence (https://www.labcanvas.com/tikz-library/credits.html#packt). The code below is exactly the library's file (pdfLaTeX); its first lines name the source and the licence, and the MIT licence asks that they stay with the code. Open it in the TikZ editor from the page: https://www.labcanvas.com/tikz/examples/plots-and-charts/grouped-bar-chart

```latex
% Grouped bar chart
% Source: https://github.com/PacktPublishing/LaTeX-graphics-with-TikZ/blob/82e099639f46ee92bc622a01c739e7f7a3e87f4b/14-diagrams/13-bar-chart.tex
% Copyright (c) 2022 Packt. MIT licence: https://github.com/PacktPublishing/LaTeX-graphics-with-TikZ/blob/main/LICENSE
% The licence’s copyright and permission notice: https://www.labcanvas.com/tikz-library/credits.html
\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\usepackage{sansmath}
\pgfplotsset{tick label style = {font=\sansmath\sffamily}}
\begin{document}
\begin{tikzpicture}[every node/.style={font=\sffamily}]
  \begin{axis}[title  = Keyword popularity in Google trends,
    ybar,
    bar width=2mm,
    x tick label style =
      {/pgf/number format/set thousands separator={}},
  legend pos=north west,
  legend cell align=left]
  \addplot coordinates { (2007,16) (2008,19) (2009,30) (2010,36)
    (2011,42) (2012,48) (2013,55)};
  \addplot coordinates { (2007,39) (2008,28) (2009,24) (2010,19)
    (2011,15) (2012,12) (2013,8)};
  \addplot coordinates { (2007,22) (2008,13) (2009,11) (2010,8)
    (2011,6) (2012,4) (2013,4)};
  \legend{tikz, pstricks, metapost}
  \end{axis}
\end{tikzpicture}
\end{document}
```

## pgfplots with sans-serif tick labels

*Lines 5–10 of the code.*

A `standalone` page with a 10pt border loads `pgfplots` and `sansmath`. Tick labels are typeset in maths mode, so to make them sans-serif the file sets `tick label style = {font=\sansmath\sffamily}`; `every node/.style={font=\sffamily}` on the picture does the same for the title and the legend, which are nodes too. There is no `\pgfplotsset{compat=…}` line, so pgfplots runs in its backwards-compatible mode (and says so in the log); adding `compat=1.18` is good practice, as in the variations.

## ybar and the bar width

*Lines 11–13 of the code.*

`ybar` given to the axis does three things: every plot is drawn as vertical bars, the bars of the different plots at the same x are placed next to each other (2pt apart by default) instead of on top of each other, and the bar cycle list gives each plot a fill and a matching border colour; it also makes the legend show small bars. `bar width=2mm` keeps the bars thin, so seven groups of three fit in the default axis width. The title is an axis option, `title = Keyword popularity in Google trends`.

## Years without a thousands separator

*Lines 14–17 of the code.*

The x values are the years as numbers, and pgfplots formats numbers with a thousands separator, which would print 2,007. `x tick label style = {/pgf/number format/set thousands separator={}}` sets the separator to nothing for the x tick labels only. An alternative is to treat the years as text with `symbolic x coords`, as in the first variation. `legend pos=north west` puts the legend in the top-left corner, where the bars are low at the start of the trend, and `legend cell align=left` aligns the entries on the left.

## Three data series and the legend

*Lines 18–24 of the code.*

Each series is `\addplot coordinates { (2007,16) (2008,19) … };`, a list of (x, y) pairs written into the file. The plots have no options, so their colours come from the bar cycle list in order: blue, red, then brown. pgfplots chooses the x ticks itself, one per year here, and centres each group of bars on its year. `\legend{tikz, pstricks, metapost}` names the plots in the order they were added. For more data, `\addplot table` reads the same numbers from a file or an inline table, which keeps long series out of the drawing code and lets a spreadsheet export feed the chart directly.

## Techniques used

- [Bar charts with pgfplots](https://www.labcanvas.com/tikz/plots): `ybar`, `bar width` and data from `\addplot coordinates`.
- [Legends](https://www.labcanvas.com/tikz/plots): `\legend`, `legend pos` and `legend cell align`.
- [Number formatting in tick labels](https://www.labcanvas.com/tikz/plots): `/pgf/number format/set thousands separator={}` for years.
- [Colours for data](https://www.labcanvas.com/tikz/colors): the default bar cycle list and a colour-blind-safe replacement in the variations.

## Variations

Our own code, CC0 1.0, each typeset with TeX Live 2026 in a standalone document with the preamble lines shown.

### Years as categories, values on the bars

Every other year, with the years as categories: `symbolic x coords={2007,2009,2011,2013}` treats them as text, so no number formatting is involved, and `xtick=data` puts one tick under each group. `nodes near coords` prints each value above its bar, the legend moves above the plot in one row, and a `cycle list` gives the three series colour-blind-safe fills with darker borders. `axis lines*=left` drops the top and right frame.

![A grouped bar chart for 2007, 2009, 2011 and 2013 with three bars per year in blue, orange and teal, each labelled with its value (16, 39, 22 in 2007 up to 55, 8, 4 in 2013), a one-row legend tikz, pstricks, metapost above the plot, and the y axis labelled relative interest.](https://www.labcanvas.com/tikz-snippets/fig-grouped-bar-chart/symbolic-labels.webp)

*the chart with symbolic x coordinates, value labels on the bars and a one-row legend*

Preamble: `\usepackage{tikz}`, `\usepackage{pgfplots}`, `\pgfplotsset{compat=1.18}`

```latex
\begin{tikzpicture}
  \begin{axis}[
      ybar=1pt, bar width=8pt, width=10cm, height=6cm,
      % the years as text: no number formatting, one tick per category
      symbolic x coords={2007,2009,2011,2013}, xtick=data, enlarge x limits=0.15,
      ymin=0, ymax=65, ylabel={relative interest}, axis lines*=left,
      nodes near coords, nodes near coords style={font=\tiny},
      legend style={at={(0.5,1.03)}, anchor=south, legend columns=-1, draw=none,
                    /tikz/every even column/.append style={column sep=3mm}},
      cycle list={{fill=blue!55, draw=blue!80!black}, {fill=orange!70, draw=orange!80!black},
                  {fill=teal!55, draw=teal!80!black}},
    ]
    \addplot coordinates {(2007,16) (2009,30) (2011,42) (2013,55)};
    \addplot coordinates {(2007,39) (2009,24) (2011,15) (2013,8)};
    \addplot coordinates {(2007,22) (2009,11) (2011,6) (2013,4)};
    \legend{tikz, pstricks, metapost}
  \end{axis}
\end{tikzpicture}
```

### The same data as lines

The data are a trend over time, which lines show more directly than bars. Without `ybar` the same `\addplot coordinates` become lines with markers; each series gets a colour and a marker shape from the `cycle list`, so the chart also works in greyscale, and the names are written at the end of each line with `node[pos=1, right]` instead of a legend (`clip=false` lets them extend past the axis).

![A line chart from 2007 to 2013 with a light grid: a blue line with dots labelled tikz rising from 16 to 55, an orange line with squares labelled pstricks falling from 39 to 8, and a teal line with triangles labelled metapost falling from 22 to 4.](https://www.labcanvas.com/tikz-snippets/fig-grouped-bar-chart/trend-lines.webp)

*the three series as lines with markers and direct labels at their ends*

Preamble: `\usepackage{tikz}`, `\usepackage{pgfplots}`, `\pgfplotsset{compat=1.18}`

```latex
\begin{tikzpicture}
  \begin{axis}[
      width=9cm, height=6cm, ymin=0, ymax=60, clip=false,
      xtick={2007,...,2013}, x tick label style={/pgf/number format/1000 sep={}},
      ylabel={relative interest}, grid=major, grid style={gray!25},
      every axis plot/.append style={very thick, mark size=2pt},
      cycle list={{blue!70!black, mark=*}, {orange!85!black, mark=square*},
                  {teal, mark=triangle*}},
    ]
    % direct labels at the end of each line instead of a legend
    \addplot coordinates {(2007,16) (2008,19) (2009,30) (2010,36) (2011,42) (2012,48) (2013,55)}
      node[pos=1, right] {tikz};
    \addplot coordinates {(2007,39) (2008,28) (2009,24) (2010,19) (2011,15) (2012,12) (2013,8)}
      node[pos=1, right, yshift=3pt] {pstricks};
    \addplot coordinates {(2007,22) (2008,13) (2009,11) (2010,8) (2011,6) (2012,4) (2013,4)}
      node[pos=1, right, yshift=-3pt] {metapost};
  \end{axis}
\end{tikzpicture}
```

## Frequently asked questions

### How do I make a grouped bar chart in pgfplots?

Give the axis the ybar option and add one \addplot per series: \begin{axis}[ybar, bar width=4pt] \addplot coordinates {(1,3) (2,5)}; \addplot coordinates {(1,4) (2,2)}; \end{axis}. ybar places the bars of the series side by side.

### How do I stop pgfplots printing 2,007 for years?

Turn off the thousands separator for the x tick labels: x tick label style={/pgf/number format/1000 sep={}}, or treat the years as text with symbolic x coords={2007,2008,…} and xtick=data.

### How do I show the values on top of the bars?

Add nodes near coords to the axis options; nodes near coords style={font=\tiny} makes the numbers smaller, and enlarge y limits or a larger ymax leaves room for them.

## References

- [PacktPublishing/LaTeX-graphics-with-TikZ at commit 82e0996: 14-diagrams/13-bar-chart.tex (MIT licence)](https://github.com/PacktPublishing/LaTeX-graphics-with-TikZ/blob/82e099639f46ee92bc622a01c739e7f7a3e87f4b/14-diagrams/13-bar-chart.tex)
- [pgfplots manual, §4.5.4 Bar Plots (ybar, bar width, bar shift)](https://tikz.dev/pgfplots/reference-2dplots#sec-4.5.4)
- [pgfplots manual, §4.3.1 Coordinate Lists and §4.3.2 Reading Coordinates From Tables](https://tikz.dev/pgfplots/reference-addplot#sec-4.3.1)
- [pgfplots manual, §4.9.4 Legends and §4.9.5 Legend Appearance](https://tikz.dev/pgfplots/reference-axisdescription#sec-4.9.4)
- [pgfplots manual, §4.21.1 String Symbols as Input Coordinates (symbolic x coords)](https://tikz.dev/pgfplots/reference-symbolic-transformations#sec-4.21.1)
- [pgfplots manual, §4.7.7 Cycle Lists](https://tikz.dev/pgfplots/reference-markers#sec-4.7.7)
- [pgfplots manual, §4.17.3 Placing Nodes on Coordinates of a Plot](https://tikz.dev/pgfplots/reference-annotations#sec-4.17.3)
- [PGF/TikZ manual, §97 Number Printing (set thousands separator, 1000 sep)](https://tikz.dev/math-numberprinting)
- [sansmath package on CTAN](https://ctan.org/pkg/sansmath)

## TikZ guides for these techniques

- [TikZ plots with pgfplots: functions, data, scatter, bars and 3D](https://www.labcanvas.com/tikz/plots): Plot in LaTeX with pgfplots: functions, data tables, scatter plots coloured by value, error bars, bar charts, log axes, 3D surfaces and groupplots.
- [TikZ colors: names, mixing, fill, shading and patterns](https://www.labcanvas.com/tikz/colors): TikZ colours with xcolor: every named colour (base and dvipsnames charts), red!30 mixing, custom colours, fill and filldraw, opacity, shading, patterns.
