Skip to main content
Albero Academy

Bar Charts

Compare values across categories — vertical, horizontal and grouped.

Core Plot Types9 min

A bar chart compares a numeric value across categories. Bar length encodes the value, so the eye compares heights easily — the right choice whenever the x-axis is categorical.

Vertical & horizontal

bar and barh
ax.bar(categories, values) # vertical
ax.barh(categories, values) # horizontal — good for long labels
ax.bar(categories, values, color="steelblue")

Grouped & stacked

comparing sub-groups
import numpy as np
x = np.arange(len(categories))
ax.bar(x - 0.2, group_a, width=0.4, label="A") # grouped
ax.bar(x + 0.2, group_b, width=0.4, label="B")
ax.set_xticks(x); ax.set_xticklabels(categories)
ax.bar(categories, group_a, label="A") # stacked
ax.bar(categories, group_b, bottom=group_a, label="B")

Key takeaways

  1. 01ax.bar (vertical) / ax.barh (horizontal) compare values across categories.
  2. 02barh is better when category labels are long.
  3. 03Grouped bars offset positions with width; stacked bars use bottom=.
  4. 04Start bar axes at zero so lengths compare fairly.

Finished this chapter?

Mark it complete to track your progress through Matplotlib.