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
ax.bar(categories, values) # verticalax.barh(categories, values) # horizontal — good for long labelsax.bar(categories, values, color="steelblue")
Grouped & stacked
import numpy as npx = np.arange(len(categories))ax.bar(x - 0.2, group_a, width=0.4, label="A") # groupedax.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") # stackedax.bar(categories, group_b, bottom=group_a, label="B")
Key takeaways
- 01ax.bar (vertical) / ax.barh (horizontal) compare values across categories.
- 02barh is better when category labels are long.
- 03Grouped bars offset positions with width; stacked bars use bottom=.
- 04Start bar axes at zero so lengths compare fairly.
