Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Practical 10: Horizontal Bar Chart for Heights

Lesson 10 of 15 in the free Data Visualisation and Analytics Lab notes on Siksha Sarovar, written by Rohit Jangra.

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:

  1. 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.
  2. 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

StudentHeight (cm)
Aman165
Riya158
Karan172
Neha160
Vikas168

Procedure

  1. Create the parallel lists students (names) and heights_cm (values) — element i of one corresponds to element i of the other.
  2. Print the pairs with zip(students, heights_cm) as the console data record.
  3. Select the Agg backend, import pyplot, and create a figure of size 7 × 4.
  4. Call plt.barh(students, heights_cm, color="teal") — names to y, heights to x.
  5. Label the axes the rotated way round: xlabel("Height (cm)"), ylabel("Student"); add the title and tight_layout().
  6. Save with plt.savefig("student_height_barh.png"); the except branch 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

  1. Swapping the axis labels — writing ylabel("Height (cm)") out of vertical-chart habit, when in barh the values run along x.
  2. Truncating the x-axis (say, starting at 150) to exaggerate the 14 cm spread — the same lie-factor sin as in Practical 5.
  3. 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

  1. When should you prefer barh over bar? When category labels are long/textual or the categories are many — horizontal labels stay readable.
  2. In plt.barh(a, b), which argument is which? a gives the y-axis categories; b gives the bar lengths along x.
  3. Which axis carries the unit label here? The x-axis — "Height (cm)" — because values extend horizontally.
  4. Who is tallest and shortest in the chart? Karan (172 cm) and Riya (158 cm).
  5. Why must the value axis start at zero? Bar length is the encoding; a cut baseline distorts every ratio between bars.
  6. How would you display the bars ranked by height? Sort the pairs first, e.g. h, s = zip(*sorted(zip(heights_cm, students))), then plt.barh(s, h).