Create a bar chart in LaTeX (pgfplots)

The option ybar of the pgfplots axis environment turns every data point into a vertical bar. symbolic x coords puts names instead of numbers on the x axis.

Preamble

The snippets on this page need these lines before \begin{document}:

\usepackage{pgfplots}
\pgfplotsset{compat=1.18}

Draw bars with category names

symbolic x coords lists the categories in the order of the axis, and the coordinates then use these names as x values. xtick=data puts a label under every bar, and ymin=0 starts the bars at zero. enlarge x limits=0.15 adds space at both ends of the x axis; without it the first and the last bar are cut in half.

\begin{tikzpicture}
\begin{axis}[
  ybar,
  symbolic x coords={Mon, Tue, Wed, Thu, Fri},
  xtick=data,
  ymin=0,
  enlarge x limits=0.15,
  ylabel={Visitors}
]
\addplot coordinates {(Mon,120) (Tue,95) (Wed,140) (Thu,110) (Fri,160)};
\end{axis}
\end{tikzpicture}
Result
The compiled example

Open in the editor

Print the values above the bars

nodes near coords prints the y value above each bar. bar width sets the width of the bars and fill their color.

\begin{tikzpicture}
\begin{axis}[
  ybar,
  symbolic x coords={Mon, Tue, Wed, Thu, Fri},
  xtick=data,
  ymin=0,
  bar width=15pt,
  enlarge x limits=0.15,
  nodes near coords
]
\addplot[fill=blue!40] coordinates {(Mon,120) (Tue,95) (Wed,140) (Thu,110) (Fri,160)};
\end{axis}
\end{tikzpicture}
Result
The compiled example

Open in the editor

Group several data series

Every further \addplot puts a bar next to the first one in each category. \legend names the series in the order of the \addplot commands.

\begin{tikzpicture}
\begin{axis}[
  ybar,
  symbolic x coords={Q1, Q2, Q3, Q4},
  xtick=data,
  ymin=0,
  bar width=10pt,
  enlarge x limits=0.2,
  legend pos=north west
]
\addplot coordinates {(Q1,30) (Q2,45) (Q3,40) (Q4,60)};
\addplot coordinates {(Q1,25) (Q2,35) (Q3,50) (Q4,55)};
\legend{2023, 2024}
\end{axis}
\end{tikzpicture}
Result
The compiled example

Open in the editor

Stack the bars

ybar stacked puts the series on top of each other instead of side by side, so the height of a bar is the sum of its parts.

\begin{tikzpicture}
\begin{axis}[
  ybar stacked,
  symbolic x coords={Q1, Q2, Q3, Q4},
  xtick=data,
  ymin=0,
  bar width=15pt,
  enlarge x limits=0.2,
  legend pos=north west
]
\addplot coordinates {(Q1,30) (Q2,45) (Q3,40) (Q4,60)};
\addplot coordinates {(Q1,25) (Q2,35) (Q3,50) (Q4,55)};
\legend{Online, Store}
\end{axis}
\end{tikzpicture}
Result
The compiled example

Open in the editor

Draw horizontal bars

For horizontal bars, write xbar and swap the roles of the axes: the names go into symbolic y coords, listed from bottom to top, and each coordinate is (value,name).

\begin{tikzpicture}
\begin{axis}[
  xbar,
  symbolic y coords={Python, Java, Rust},
  ytick=data,
  xmin=0,
  bar width=10pt,
  enlarge y limits=0.3,
  nodes near coords,
  xlabel={Answers in \%}
]
\addplot coordinates {(48,Python) (31,Java) (12,Rust)};
\end{axis}
\end{tikzpicture}
Result
The compiled example

Open in the editor

Pitfalls

Compiled with pdfLaTeX from TeX Live 2023.

Related pages