LabCanvas

Grouped bar chart in pgfplots, explained

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.

Contents

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.
Grouped bar chart, typeset with TeX Live 2026 (pdfLaTeX) from the code below, exactly as it is in PacktPublishing/LaTeX-graphics-with-TikZ.
Copyright (c) 2022 Packt · MIT licence (full text). The licence asks that this notice stay with the code: keep the first lines when you reuse it.
% 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

Variations

Our own code, written for this page and typeset with TeX Live 2026 like the figure above. Each one is a complete example: copy it, or open it in the TikZ editor and change it. This code is CC0 1.0, free to use without conditions.

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.
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}
\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}
Open in the TikZ editor
Full document
A complete file you can compile as it is.
\documentclass[border=4pt]{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\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}
\end{document}

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.
the three series as lines with markers and direct labels at their ends
Preamble: \usepackage{tikz} \usepackage{pgfplots} \pgfplotsset{compat=1.18}
\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}
Open in the TikZ editor
Full document
A complete file you can compile as it is.
\documentclass[border=4pt]{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\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}
\end{document}

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

  1. PacktPublishing/LaTeX-graphics-with-TikZ at commit 82e0996: 14-diagrams/13-bar-chart.tex (MIT licence)github.com
  2. pgfplots manual, §4.5.4 Bar Plots (ybar, bar width, bar shift)tikz.dev
  3. pgfplots manual, §4.3.1 Coordinate Lists and §4.3.2 Reading Coordinates From Tablestikz.dev
  4. pgfplots manual, §4.9.4 Legends and §4.9.5 Legend Appearancetikz.dev
  5. pgfplots manual, §4.21.1 String Symbols as Input Coordinates (symbolic x coords)tikz.dev
  6. pgfplots manual, §4.7.7 Cycle Liststikz.dev
  7. pgfplots manual, §4.17.3 Placing Nodes on Coordinates of a Plottikz.dev
  8. PGF/TikZ manual, §97 Number Printing (set thousands separator, 1000 sep)tikz.dev
  9. sansmath package on CTANctan.org

The walkthrough was checked against these sources and the pictures are the code typeset with TeX Live 2026. The original figure is © Packt (code from LaTeX Graphics with TikZ by Stefan Kottwitz), under the MIT licence; the variations on this page are ours, CC0 1.0.

TikZ guides for these techniques

More figures explained

Change this figure in your browserOpen the figure or any variation in the free TikZ editor: it is typeset with TeX Live as you type, with errors pointed at their line.

Open the figure in the TikZ editor