Aim
To store a student's marks in 10 unit tests in a list marks and plot them as a line chart with plt.plot(), using markers, axis labels and a grid so the trend across tests is readable.
CO Mapping: CO1, CO2
Theory
A line chart encodes an ordered sequence: the x-axis carries an inherently ordered variable (here, test number 1–10) and connecting the points with line segments encodes continuity — the claim that it is meaningful to travel from one observation to the next. That is why lines suit time series and sequences but are wrong for unordered categories (connecting "Delhi" to "Pune" with a line asserts an order that does not exist; that is bar-chart territory, as in Practical 5).
The eye reads three things from a line chart, in order of salience:
- Slope — the local direction of change between consecutive tests; the steeper the segment, the bigger the jump.
- Trend — the overall drift once local wiggles are smoothed away.
- Outliers/dips — points that break the prevailing pattern and deserve a causal question.
Craft choices in the snippet serve those readings: marker="o" shows where the actual data lives (the line between markers is interpolation, not measurement); plt.xticks(tests) forces one tick per test so matplotlib cannot invent fractional test numbers like 2.5; and plt.grid(True, alpha=0.3) adds faint rulings for value look-up without shouting over the data. As in Practical 5, matplotlib.use("Agg") is selected before importing pyplot so the script renders to a PNG file even on headless machines.
Dataset
| Test | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
|---|---|---|---|---|---|---|---|---|---|---|
| Marks | 62 | 68 | 71 | 75 | 70 | 78 | 82 | 80 | 84 | 89 |
Procedure
- Create the list
markswith the 10 scores andtests = list(range(1, 11))for the x-axis — the two lists must be equal in length. - Print
marksas the data record. - Inside the
tryblock, set theAggbackend, then importpyplot. - Call
plt.plot(tests, marks, marker="o", linestyle="-")— x first, y second. - Add
title,xlabel("Test Number"),ylabel("Marks"), integer ticks viaplt.xticks(tests)and a light grid. - Save the figure with
plt.savefig("unit_test_line_chart.png"); theexceptbranch reports gracefully if no plotting backend exists.
Interpretation of Results
The line climbs from 62 in test 1 to 89 in test 10 — a net gain of 27 marks, an unmistakable upward trend. The rise is not monotonic: there are two dips, test 5 (75 → 70) and test 8 (82 → 80), visible as short downward segments against the prevailing upward slope. The steepest single climb is test 5 to 6 (70 → 78, +8), a recovery immediately after the worse dip. An analyst reads this as sustained improvement with local setbacks: the trend answers "is the student improving?" (yes), while the dips prompt the follow-up questions a table of ten numbers would never surface as quickly — what happened around tests 5 and 8?
Common Mistakes
- Plotting
plt.plot(marks)alone — matplotlib then numbers the x-axis 0–9, so every "test number" in the chart is off by one. - Omitting
plt.xticks(tests)and getting fractional ticks (2.5, 7.5) for a variable that only takes integer values. - Using a line chart for categorical data — the connecting line falsely implies an ordered progression between categories.
🎯 Viva Questions
- When is a line chart appropriate? When the x-variable is ordered (time, sequence) so connecting consecutive points is meaningful.
- What do the markers add? They show the actual observations; the segments between them are visual interpolation, not data.
- What is the overall trend and net change here? Upward — from 62 to 89, a net gain of 27 marks across 10 tests.
- Which tests show a drop in performance? Test 5 (75 → 70) and test 8 (82 → 80).
- Why must
testsandmarkshave equal length?plt.plot(x, y)pairs elements positionally; unequal lengths raise a ValueError. - How would you plot two students on one chart? Call
plt.plot()twice with alabel=each, thenplt.legend().