Contents

Bar Charts

This article explains how to draw bar charts in LaTeX. The two most common forms are vertical bar charts and horizontal bar charts.

One practical package for vertical bar charts is pgfplots.

  1. Use the article document class.
\documentclass{article}
  1. Add pgfplots in the preamble.
\usepackage{pgfplots}
\pgfplotsset{width=10cm,compat=1.5}
  1. Set up the axes and chart options.
\begin{axis}[
  ybar,
  enlargelimits=0.20,
  ylabel={Average score},
  xlabel={Student name},
  symbolic x coords={Abdi,Andi,Bayu,Cindi,Clara,Diva},
  xtick=data,
  nodes near coords,
  nodes near coords align={vertical},
]
  1. Add the data points.
\addplot coordinates {
  (Abdi,65) (Andi,85) (Bayu,70)
  (Cindi,80) (Clara,60) (Diva,90)
};
\end{axis}
  1. The final result looks like this.

Vertical bar chart in LaTeX

For a horizontal bar chart, tikz works well.

  1. Keep using the article document class.
\documentclass{article}
  1. Add the required packages.
\usepackage[indonesian]{babel}
\usepackage{xcolor}
\usepackage{tikz}
  1. Define colors for the bars.
\definecolor{abdi}{HTML}{D7191C}
\definecolor{andi}{HTML}{FDAE61}
\definecolor{bayu}{HTML}{ABDDA4}
\definecolor{cindi}{HTML}{2B83BA}
\definecolor{clara}{HTML}{00FFFF}
\definecolor{diva}{HTML}{8A2BE2}
  1. Prepare the figure environment.
\begin{figure}
  ...
\end{figure}
  1. Create the drawing scale with tikzpicture.
\begin{tikzpicture}[x={(.1,0)}]
  ...
\end{tikzpicture}
  1. Draw the x and y axes.
\draw (0,0) -- (100,0);
\draw (0,0) -- (0,6.4);
  1. Add scale labels on the x axis.
\foreach \x in {10,20,...,100} {
  \draw (\x,.2) -- (\x,0) node[below] {\x};
}
  1. Add the labels and bars on the y axis.
\foreach \l/\x/\c[count=\y] in {
  Abdi/65/abdi,
  Andi/85/andi,
  Bayu/70/bayu,
  Cindi/80/cindi,
  Clara/60/clara,
  Diva/90/diva
} {
  \node[left] at (0,\y) {\l};
  \fill[\c] (0,\y-.4) rectangle (\x,\y+.4);
  \node[right] at (\x,\y) {\x};
}
  1. Add the caption.
\caption{Average Mathematics Test Score Bar Chart}
  1. The final result looks like this.

Horizontal bar chart in LaTeX

Here is a complete example for the horizontal version.

\documentclass{article}
\usepackage[indonesian]{babel}
\usepackage{xcolor}
\usepackage{tikz}

\definecolor{abdi}{HTML}{D7191C}
\definecolor{andi}{HTML}{FDAE61}
\definecolor{bayu}{HTML}{ABDDA4}
\definecolor{cindi}{HTML}{2B83BA}
\definecolor{clara}{HTML}{00FFFF}
\definecolor{diva}{HTML}{8A2BE2}

\begin{document}

\begin{figure}
\centering
\begin{tikzpicture}[x={(.1,0)}]
  \draw (0,0) -- (100,0);
  \draw (0,0) -- (0,6.4);

  \foreach \l/\x/\c[count=\y] in {
    Abdi/65/abdi,
    Andi/85/andi,
    Bayu/70/bayu,
    Cindi/80/cindi,
    Clara/60/clara,
    Diva/90/diva
  } {
    \node[left] at (0,\y) {\l};
    \fill[\c] (0,\y-.4) rectangle (\x,\y+.4);
    \node[right] at (\x,\y) {\x};
  }

  \foreach \x in {10,20,...,100} {
    \draw (\x,.2) -- (\x,0) node[below] {\x};
  }
\end{tikzpicture}
\caption{Average Mathematics Test Score Bar Chart}
\end{figure}

\end{document}

These two examples provide a solid starting point for making bar charts in LaTeX. From here, you can extend the style, colors, axis options, and labels as needed.

Related Content