1. Home
  2. Data & files

Plotting · bar charts

Bar Graphs: Uniform Bar Width, Spacing and Baselines

Eigenfield editorial team ·

A bar graph shows one number per category as the height of a rectangle. Drawing one by hand with a plotting routine is easy; getting uniform bars with sensible gaps, correct grouping and an honest axis takes a few decisions. This page sets out the geometry of uniform width, the grouping rule for side-by-side series, and the baseline convention. It is part of the plotting material in Data and files, and the broader choice of chart type is discussed under data visualization.

The geometry of uniform width

Place category i at position x = i, so the centers are 1, 2, 3 and so on. Give every bar the same width w, expressed as a fraction of the spacing between centers. With w = 0.8, the gap between neighboring bars is 1 − 0.8 = 0.2 of the spacing. Bar i then spans from i − w/2 to i + w/2, which for i = 3 gives the interval from 2.6 to 3.4.

Uniform width is not a decoration. When bars have different widths the eye reads area, not height, and a narrow bar of height 5 looks smaller than a wide bar of height 4. Keeping the width constant makes height the only variable. If the categories are numeric and unevenly spaced, such as years 1990, 1995, 2005, a bar chart with equal widths still places them by position; whether to space them evenly or in proportion to the numbers is a judgment, and a histogram is the better form for continuous data.

import numpy as np, matplotlib.pyplot as plt
heights = np.array([4.0, 7.5, 3.2, 6.1])
x = np.arange(1, len(heights) + 1)
plt.bar(x, heights, width=0.8)
plt.ylim(0, heights.max() * 1.1)

Grouped bars

To compare two or three series per category, divide the width among the series. For k series and total group width w, each bar has width w/k. With k = 2 and w = 0.8 each bar is 0.4 wide, and the offsets from the category center are −0.2 and +0.2. In general the center of series s, counted from 0, is at x + (s − (k − 1)/2) × (w/k). Check with k = 3: offsets are −w/3, 0 and +w/3, symmetric around the center as they should be.

Keep the order of the series the same in every group, and use the same color for the same series throughout. Add a legend once. Group spacing, 1 − w, should be clearly larger than the spacing between bars inside a group, which is zero here, so the groups read as groups.

The baseline

A bar encodes a value by its length from the baseline, so the baseline must be zero. Truncating the axis, for example starting at 90 for values between 92 and 98, makes a difference of 6 percent look like a difference of several times. If the interesting part of the data lies in a narrow range far from zero, a dot plot or a line is the honest alternative, because position along a common scale does not require a zero origin.

Negative values extend below the baseline; draw a visible zero line and place labels where they do not collide with the bars.

Labels and order

Sort categories by value unless they have a natural order such as months. Put long labels on the vertical axis by switching to horizontal bars, so the text stays horizontal and readable. When exact values matter, print the number at the end of each bar instead of asking the reader to estimate from gridlines.

Counted data fits bar graphs well, while the distribution of a continuous variable calls for a histogram. When a plot needs a second dimension, contour levels take over where bars stop being readable.

Back to top