TExcelTAD
Home › Statistics › Bar Graphs

Bar Graphs

A bar graph (bar chart) shows categorical data: one bar per category, with the bar's length proportional to its frequency (or to another value such as total sales). The bars are separated by gaps to show that the categories are distinct, which is the visual difference from a histogram.

Bar graph Histogram
Data categories (or discrete values) continuous numbers grouped into bins
Gaps between bars yes no
Can reorder bars? yes (often sorted by size) no, the axis is a number line

A simple bar graph

A bakery counted which bread its 120 customers bought on Saturday:

python · try it yourself
import matplotlib.pyplot as plt

breads = ["sourdough", "rye", "baguette", "whole wheat", "brioche"]
sold = [38, 14, 31, 22, 15]

fig, ax = plt.subplots(figsize=(6, 3.5))
ax.bar(breads, sold, color="#2a78d6", width=0.6)
ax.set_ylabel("loaves sold")
ax.set_title("Saturday bread sales")
for x, y in zip(breads, sold):
    ax.annotate(str(y), (x, y), ha="center", va="bottom", fontsize=9)
plt.tight_layout()
plt.show()
print("Bar chart of", sum(sold), "loaves in", len(breads), "categories")
Bar chart of 120 loaves in 5 categories

Sort the bars

For nominal categories (no natural order), sorting bars by size makes comparisons much easier. Horizontal bars also leave room for long labels:

python · try it yourself
import pandas as pd
import matplotlib.pyplot as plt

sales = pd.Series({"sourdough": 38, "rye": 14, "baguette": 31,
                   "whole wheat": 22, "brioche": 15}).sort_values()

fig, ax = plt.subplots(figsize=(6, 3))
ax.barh(sales.index, sales.values, color="#2a78d6", height=0.6)
ax.set_xlabel("loaves sold")
plt.tight_layout()
plt.show()
print(sales.to_string())
rye            14
brioche        15
whole wheat    22
baguette       31
sourdough      38

If the categories are ordinal (e.g. "poor, fair, good, excellent"), keep their natural order instead of sorting by size.

Always start at zero

A bar's length encodes the value, so the value axis must start at 0. Cutting the axis makes small differences look huge:

python · try it yourself
import matplotlib.pyplot as plt

shops = ["North", "South"]
customers = [1020, 1050]

fig, (honest, misleading) = plt.subplots(1, 2, figsize=(8, 3))
honest.bar(shops, customers, color="#2a78d6", width=0.5)
honest.set_title("axis from 0")
misleading.bar(shops, customers, color="#eb6834", width=0.5)
misleading.set_ylim(1000, 1055)
misleading.set_title("axis from 1000 (misleading)")
fig.tight_layout()
plt.show()
print(f"South has {customers[1] / customers[0] - 1:.1%} more customers than North")
South has 2.9% more customers than North

A difference of under 3% looks like South has several times as many customers in the right-hand chart. This is one of the most common ways graphs mislead.

Bar graphs of discrete numbers

Discrete numeric data with few distinct values, such as the number of goals per match, is also shown as separated bars, one for each value:

python · try it yourself
import numpy as np
import matplotlib.pyplot as plt

goals = [0, 2, 1, 3, 1, 0, 2, 2, 4, 1, 1, 0, 3, 2, 1, 5, 2, 1, 0, 2]
values, counts = np.unique(goals, return_counts=True)

fig, ax = plt.subplots(figsize=(5, 3))
ax.bar(values, counts, color="#2a78d6", width=0.6)
ax.set_xticks(values)
ax.set_xlabel("goals in a match")
ax.set_ylabel("matches")
plt.tight_layout()
plt.show()
for v, c in zip(values, counts):
    print(f"goals = {v}: {c} matches")
goals = 0: 4 matches
goals = 1: 6 matches
goals = 2: 6 matches
goals = 3: 2 matches
goals = 4: 1 matches
goals = 5: 1 matches

Grouped bars: comparing two variables

A grouped bar chart shows a contingency table: each category of one variable gets a cluster of bars, one per category of the other. pandas draws one from a DataFrame:

python · try it yourself
import pandas as pd
import matplotlib.pyplot as plt

table = pd.DataFrame({"weekday": [52, 18, 30], "weekend": [41, 45, 12]},
                     index=["city bike", "e-bike", "cargo bike"])
ax = table.plot.bar(rot=0, color=["#2a78d6", "#eb6834"], figsize=(6, 3.5), width=0.7)
ax.set_ylabel("rentals")
plt.tight_layout()
plt.show()
print(table)
            weekday  weekend
city bike        52       41
e-bike           18       45
cargo bike       30       12

E-bikes dominate on weekends while city bikes dominate on weekdays, a pattern that would be hard to see in the raw rental log.

Test yourself

What kind of data is a bar graph mainly used for?

  1. Continuous measurements grouped into bins
  2. Categorical (or discrete) data
  3. Pairs of numbers
  4. Time of day only

Why should the value axis of a bar graph start at 0?

  1. Matplotlib requires it
  2. Bar length encodes the value, so a cut axis exaggerates differences
  3. It makes the chart smaller
  4. Zero is the mean

When should you NOT sort bars by size?

  1. When categories are ordinal, like 'poor, fair, good'
  2. When there are more than 3 bars
  3. When using horizontal bars
  4. Never