Bar Charts
You have mastered the art of seeing trends with line plots. Now let's switch gears to bar charts — your secret weapon for comparing things side by side.
Your school's annual competition has just wrapped up, and everyone wants to know which house — Shivaji, Tagore, Ashoka or Raman — has claimed victory. How do you present those final scores in a way that is clear, impactful and understandable at a glance? Enter the bar chart.
1The blueprint: what are we building?
Before writing a single line of code, let's play detective and analyze our target chart:
- The big idea (title)— “House Points in Annual Competition”.
- The “who” (X-label)— “House Name”, explaining the names along the bottom.
- The “how many” (Y-label)— “Points Scored”, explaining the numbers up the side.
- The players (categories) — Shivaji, Tagore, Ashoka, Raman.
- The scores (values) — 180, 220, 150, and Raman the champion at 250.
2The coding canvas: bringing bars to life
plt.bar() is the core instruction. It takes two main arguments: the categories that go on the X-axis, and the values that decide how tall each bar should be.
0. Start the Y-axis at, say, 140 and Raman's bar looks five times taller than Ashoka's instead of 1.7× — the chart would be lying. Line charts may crop their Y-axis; bar charts may not.3Customizing colours and widths
plt.bar() accepts color and width arguments, and they follow one elegant rule:
- Give a single value → every bar gets it.
- Give a list → each bar gets its own, matched by position.
Play with both modes below and watch the plt.bar() call rewrite itself.
Pass one value to style every bar the same; pass a list to style each bar individually.
house_names = ['Shivaji', 'Tagore', 'Ashoka', 'Raman'] house_points = [180, 220, 150, 250] plt.bar(house_names, house_points)
With no styling arguments, pyplot picks its own blue and a width of 0.8.
The two customizations from your textbook are just the two modes of that rule:
4Recap
| Argument | Single value | List |
|---|---|---|
| color | color='red' → all bars red | color=['red','green',…] → one each |
| width | width=0.4 → all bars 0.4 wide | width=[0.4, 0.3, …] → one each |
| Default | pyplot's blue, width 0.8 | — |
Which function creates a vertical bar chart?
plt.bar(names, points, color=['red', 'green']) with four bars will…
Why must a bar chart's Y-axis start at zero?