Aim
To plot a horizontal bar chart of five students' heights using plt.barh(), with student names on the y-axis and height in centimetres on the x-axis.
CO Mapping: CO1, CO2, CO3
Theory
A horizontal bar chart is the vertical bar chart of Practical 5 rotated 90°: categories run down the y-axis and the value is encoded as bar length along x. The perceptual machinery is identical — length judged against a common zero baseline, the channel humans decode most accurately — so the same integrity rule applies: the value axis must start at 0, otherwise the length ratios lie.
Why choose horizontal over vertical? Two practical reasons:
- Label legibility. Category labels like student names sit horizontally beside the bars and are read naturally; on a vertical chart, long names must be rotated or truncated. This is the standard advice whenever labels are text rather than short codes, or when there are many categories.
- Reading direction. Comparing bar lengths along the horizontal matches the left-to-right reading habit, and ranked lists ("top N by value") scan naturally top-to-bottom.
In matplotlib the switch is a single function swap — plt.barh(students, heights_cm) instead of plt.bar() — but the axis roles invert with it: the first argument now labels the y-axis and the numeric values extend along x. Consequently xlabel must carry the unit ("Height (cm)") and ylabel the category name ("Student") — the exact opposite of Practical 5, and a classic source of mislabelled charts. One caveat specific to this data: heights cluster in a narrow band (158–172 cm), so from a zero baseline the bars look similar in length. That is honest — the students really are similar in height — and resisting the urge to truncate the axis to dramatise a 14 cm spread is the discipline this practical trains.
Dataset
| Student | Height (cm) |
|---|---|
| Aman | 165 |
| Riya | 158 |
| Karan | 172 |
| Neha | 160 |
| Vikas | 168 |
Procedure
- Create the parallel lists
students(names) andheights_cm(values) — element i of one corresponds to element i of the other. - Print the pairs with
zip(students, heights_cm)as the console data record. - Select the
Aggbackend, importpyplot, and create a figure of size 7 × 4. - Call
plt.barh(students, heights_cm, color="teal")— names to y, heights to x. - Label the axes the rotated way round:
xlabel("Height (cm)"),ylabel("Student"); add the title andtight_layout(). - Save with
plt.savefig("student_height_barh.png"); theexceptbranch handles headless environments without a backend.
Interpretation of Results
Karan's bar extends furthest (172 cm) and Riya's least (158 cm) — a spread of only 14 cm across the five students. Vikas (168) and Aman (165) sit just behind Karan, with Neha (160) nearer Riya. Because every bar grows from 0, the visual message is honest: all five bars are nearly the same length, correctly conveying that these heights differ by under 9% of their magnitude. Matplotlib plots the first list element at the bottom, so the chart reads Aman at the bottom up to Vikas at the top — sorting heights_cm (with names kept in step) before plotting would turn the chart into an instantly readable ranking, a common refinement.
Common Mistakes
- Swapping the axis labels — writing
ylabel("Height (cm)")out of vertical-chart habit, when inbarhthe values run along x. - Truncating the x-axis (say, starting at 150) to exaggerate the 14 cm spread — the same lie-factor sin as in Practical 5.
- Sorting one list but not the other — names and heights are parallel lists, so any reordering must be applied to both together (e.g. via
sorted(zip(...))).
🎯 Viva Questions
- When should you prefer
barhoverbar? When category labels are long/textual or the categories are many — horizontal labels stay readable. - In
plt.barh(a, b), which argument is which?agives the y-axis categories;bgives the bar lengths along x. - Which axis carries the unit label here? The x-axis — "Height (cm)" — because values extend horizontally.
- Who is tallest and shortest in the chart? Karan (172 cm) and Riya (158 cm).
- Why must the value axis start at zero? Bar length is the encoding; a cut baseline distorts every ratio between bars.
- How would you display the bars ranked by height? Sort the pairs first, e.g.
h, s = zip(*sorted(zip(heights_cm, students))), thenplt.barh(s, h).