LambdaLab
Informatics Practices · Class 12 · Data Visualization
Data VisualizationHands-on⏱️ 8 min read

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.

house_points.py
Bars must start at zero
A bar's height is what the reader compares, so the baseline has to 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.

Style your bars

Pass one value to style every bar the same; pass a list to style each bar individually.

050100150200250ShivajiTagoreAshokaRamanHouse Points in Annual CompetitionHouse NamePoints Scored
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:

bar_styles.py
Colour should mean something
Giving each bar a different colour looks lively, but if the colours don't encode anything, they are decoration that the reader has to decode for nothing. Reach for different colours when you want to highlight one bar — the winning house, say — and leave the rest a calm single colour.

4Recap

ArgumentSingle valueList
colorcolor='red' → all bars redcolor=['red','green',…] → one each
widthwidth=0.4 → all bars 0.4 widewidth=[0.4, 0.3, …] → one each
Defaultpyplot's blue, width 0.8
Quick Check

Which function creates a vertical bar chart?

Quick Check

plt.bar(names, points, color=['red', 'green']) with four bars will…

Quick Check

Why must a bar chart's Y-axis start at zero?