Bar charts

Intermediate

Introduction

The bar chart is the workhorse of data visualization. It encodes each value as the length of a bar from a common baseline, the channel people judge most accurately after position. A well-made bar chart answers "which is biggest, and by how much?" faster than any other form. This lesson builds bar charts as text, which shows exactly what a charting library computes for you.

How it works

Each bar's length is its value scaled to the available width: length = value / largest × width. Because a length is a whole number of characters (or pixels), the result is rounded; rounding half up with int(x + 0.5) gives the same answer every time, where Python's round rounds halves to the nearest even number.

The baseline must be zero. A bar's length means "this much", so if the axis starts at 50, a value of 60 gets a bar twice as long as a value of 55, a false story. That rule is specific to bars and areas; the scales lesson explains why lines may start elsewhere.

Sort the bars by value when the point is ranking: the reader then sees the order without comparing every pair. Keep the data's own order when it has one, such as months, age groups or survey answers from "never" to "always". With many categories or long names, use a horizontal bar chart: labels sit comfortably on the left and the bars still share a baseline. Writing each value at the end of its bar, a direct label, saves the reader from reading across to an axis.

To compare two or more series, there are two layouts. Grouped bars place the series side by side in each category, which makes it easy to compare 2023 with 2024 for one region. Stacked bars place them end to end, which shows each category's total and its parts, but only the first segment has a common baseline, so the middle segments are hard to compare. Use the same scale for every bar in a chart, or the comparison means nothing.

In Matplotlib, plt.barh(names, values) draws horizontal bars, ax.bar_label(bars) adds direct labels, and grouped bars are drawn by shifting each series' x positions by a bar width.

Example

# A horizontal bar chart as text: sorted, scaled to a fixed width, labelled.
sales = {"North": 42, "South": 18, "East": 35, "West": 27, "Central": 9}
WIDTH = 30

ranked = sorted(sales.items(), key=lambda kv: (-kv[1], kv[0]))
biggest = ranked[0][1]
label_width = max(len(name) for name in sales)
for name, value in ranked:
    length = int(value / biggest * WIDTH + 0.5)  # round half up
    print(f"{name:>{label_width}} | {'█' * length} {value}")
Open in compiler

Line by line

  1. The data is a dictionary of region to sales.
  2. sorted with the key (-value, name) ranks from largest to smallest, breaking ties by name.
  3. Each bar's length is its share of the largest value times 30 characters, rounded half up.
  4. Labels are right-aligned to the longest name, so every bar starts in the same column: the baseline.

Expected result

  North | ██████████████████████████████ 42
   East | █████████████████████████ 35
   West | ███████████████████ 27
  South | █████████████ 18
Central | ██████ 9

In the real world

Election results, survey answers, sales by product and app ratings are almost always shown as bars. Style guides at news organisations require sorted bars and a zero baseline, and many forbid 3D bars, whose perspective makes lengths impossible to compare. Dashboards use horizontal bars for "top 10" lists.

Common mistakes

  • A baseline above zero, which exaggerates differences between bars.
  • Unsorted bars when the point is a ranking.
  • 3D or shadowed bars, which distort lengths.
  • Different scales for bars in the same chart, for example two panels side by side with their own axes.

Best practices

  • Start at zero, sort by value, and label values directly.
  • Use horizontal bars for long labels or more than about seven categories.
  • Limit stacked bars to a few segments, with the most important one at the baseline.

Summary

A bar chart encodes values as lengths from a zero baseline, scaled to the available width and rounded consistently. Sort bars to show a ranking, turn them horizontal for long labels, and compare series with grouped bars or show parts of totals with stacked bars, always on one shared scale.

Quick check

1. Why must a bar chart start at zero?
2. When should bars not be sorted by value?
3. What do stacked bars show well?
4. Which rounding gives 3.5 → 4 every time?

Key takeaways

  • A bar's length is proportional to its value from a zero baseline.
  • Sort bars by value unless the categories have their own order.
  • Grouped bars compare series within a category; stacked bars show the parts of a total.

Practice problems for this lesson

My notes

Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.