Lesson 7: Bar Charts & Histograms

⏱ ~25 min Lesson 7 of 10 💚 Free

Bar charts compare values across categories. Histograms show how data is distributed across ranges. Both use bars — but they answer very different questions.

Key Concepts

Bar Chart vs Histogram

Bar chart: each bar = one category (apples, oranges, grapes). Bars are separate. Histogram: each bar = a range of values (0-10, 10-20...). Bars touch. Bar charts compare; histograms show distribution.

plt.bar() and plt.barh()

plt.bar(categories, values, color="steelblue") # vertical
plt.barh(categories, values) # horizontal
Add error bars: plt.bar(x, y, yerr=std_devs, capsize=5)

Grouped & Stacked Bars

For multiple series: shift x positions by the bar width.
x = range(len(categories))
plt.bar([i-0.2 for i in x], series1, width=0.4)
plt.bar([i+0.2 for i in x], series2, width=0.4)

plt.hist()

plt.hist(data, bins=10, color="purple", edgecolor="white")
bins controls the number of buckets. More bins = more detail. Fewer = smoother shape.

🔬 Interactive Lab: Bar Chart Customizer

Edit the label and value, add bars, and choose a color scheme. Build your own comparison chart.

✅ Check Your Understanding

1. What is the key difference between a bar chart and a histogram?

2. In plt.hist(), what does the bins parameter control?

3. Which chart would best show how many students scored in each 10-point range?