Theory
Nobody reads the numbers table
ResultDesk's describe() output is statistically perfect, and the principal's eyes glaze over in four seconds flat.
Then you show a single line sloping downward: DBMS averages falling across three internal tests, and the room leans in. Same data. Different container.
Humans process pictures hundreds of times faster than tables. Unit 5 is about that conversion, and its engine is Python's standard plotting library: matplotlib.
Theory
A graph paper workbench
Think of pyplot as a graph-paper workbench:
plt.plot() pencils a line onto the current sheet. Call it again: another line, same sheet. title/xlabel/ylabel write the headings. legend() pins the colour key. And nothing leaves the workbench until plt.show() holds the sheet up.
subplot() is the power move: rule the sheet into a grid and draw a different chart in each cell.
Theory
The pyplot vocabulary
import matplotlib.pyplot as plt: the universal alias (pip install matplotlib once).
- plt.plot(x, y): draw a 2-D line through the points.
- plt.title / plt.xlabel / plt.ylabel: name the chart and both axes.
- plt.legend(): the key: built from each plot's
label=argument. - plt.show(): render the figure: scripts show NOTHING without it.
- plt.savefig('report.png'): save instead of show.
Helpers you already know: range(n) makes x positions 0..n-1, len(df) counts rows, df.columns lists what is plottable.
Practical
First plot, then a 1x2 grid
import matplotlib.pyplot as plt
import pandas as pd
df = pd.read_csv('marks.csv')
# one chart: DBMS scores across roll numbers
dbms = df[df['subject'] == 'DBMS']
plt.plot(dbms['roll'], dbms['score'], label='DBMS')
plt.plot(dbms['roll'], dbms['score'] * 0 + dbms['score'].mean(),
label='class mean') # a flat reference line
plt.title('DBMS scores by roll')
plt.xlabel('Roll number')
plt.ylabel('Score')
plt.legend() # uses the label= texts
plt.show() # nothing appears without this
# two charts on ONE figure: subplot(rows, cols, index), index from 1
plt.figure(figsize=(10, 4))
plt.subplot(1, 2, 1) # left cell
plt.plot(range(len(dbms)), dbms['score'])
plt.title('By position (range/len)')
plt.subplot(1, 2, 2) # right cell
plt.plot(dbms['roll'], dbms['score'])
plt.title('By roll number')
plt.show()
This example runs in Gri-Learn on the web, where you can edit it and see the output.
Think first
The blank-window mystery
A student's script builds a beautiful plot: plot(), title(), labels, legend()... and running it prints nothing, shows nothing, no error. Before tapping: what single line is missing, and what were all those earlier calls actually doing?
Show the answer
plt.show() is missing. Every earlier call drew on an in-memory figure: the workbench sheet that never got held up. The script ends, the figure is discarded, silently.
(In Jupyter notebooks figures often display automatically, which is exactly why students forget show() in plain scripts: the exam answer is: pyplot builds figures in memory; show() renders them.)
Quiz
plt.subplot(2, 2, 1) is called. Which cell of the figure does the next plot land in?
- Top-left: a 2x2 grid, and index 1 is the FIRST cell counting row-wise from 1
- Bottom-right: index counts backwards
- Top-left, because index 0 means first
- It errors: subplot needs an index from 0
Show the answer
Top-left: a 2x2 grid, and index 1 is the FIRST cell counting row-wise from 1
subplot(rows, cols, index) rules a rows x cols grid and selects cell number index, counting from 1, left to right, top to bottom: (2,2,1) is top-left, (2,2,2) top-right, (2,2,3) bottom-left, (2,2,4) bottom-right. The from-1 counting trips Python instincts trained on zero-based lists: options C and D are exactly that instinct, and exactly the lost mark.
Watch out
The three chart-killers
No plt.show(): silence instead of a chart (scripts, not notebooks).
No label= arguments: plt.legend() draws an empty box or warns: every plot() you want in the key needs label='...'.
Unlabelled axes: examiners deduct for a chart with no title/xlabel/ylabel, and they are right: an unlabelled chart is a rumour, not a report. The syllabus also names ax.set_title(): the object-style spelling of the same idea, used with fig, ax = plt.subplots().
Theory
One pattern, four charts ahead
Every remaining lesson in this unit is the SAME skeleton with one verb swapped: scatter() for point clouds, plot() for trends, hist() for distributions, bar() for category comparisons. Learn the skeleton once (draw, title, label axes, legend, show) and the next four lessons are each one new verb plus the judgement of WHEN to use it.
Summary
Key takeaways
- import matplotlib.pyplot as plt; pip-installed like pandas.
- Skeleton: plot(x, y, label=...), title(), xlabel(), ylabel(), legend(), show().
- Nothing renders without plt.show() in scripts; savefig() writes to a file.
- Repeated plot() calls layer lines on one figure; legend() keys them by label=.
- subplot(rows, cols, index) tiles charts: index counts from 1, row-major.
- range() and len() build x positions; DataFrame columns plot directly.
- Memory hook: pencil on the sheet, hold it up with show().