Bar Chart vs. Histogram
They look similar but serve different purposes:
- Bar chart: compares distinct categories (e.g., subjects, cities). Bars do not touch.
- Histogram: shows the distribution of one continuous numerical variable grouped into bins (e.g., how many students scored 80–90). Bars touch.
💡
Quick Rule
If your x-axis has labels like "Math, Science, English" → use plt.bar(). If your x-axis has a number range like "60–100 scores" → use plt.hist().
Creating a Bar Chart
import matplotlib.pyplot as plt
subjects = ["Math", "Science", "Art"]
scores = [88, 92, 96]
plt.bar(subjects, scores, color="steelblue")
plt.title("Average Scores by Subject")
plt.ylabel("Score")
plt.show()
Creating a Histogram
scores = [72, 85, 91, 88, 76, 95, 83, 79, 88, 92, 67, 84]
plt.hist(scores, bins=5, color="teal", edgecolor="white")
plt.title("Score Distribution")
plt.xlabel("Score")
plt.ylabel("Number of Students")
plt.show()
🆕
Choosing Bins
Too few bins hides detail; too many bins makes the chart noisy. For class sizes of 20–50 students, 5–10 bins usually works well.