Programming and Computation · Module 9 of 10

Plotting and Visualizing Data

A number is hard to judge; a curve is easy. This module covers plotting with matplotlib: building x and y arrays, drawing a line or scatter plot, always labeling axes with units, and reading the picture to check that a result makes sense.

01

Readiness check

This module turns data into pictures. Tick only what you can do closed-notes.

  • Build an array with linspace from Module 8.
  • Evaluate a formula over an array.
  • Recall the axes of an x-y graph.
  • Recall why axis labels and units matter.
  • Import a module from Module 6.
0 or 1 weak itemsContinue with this module.
2 weak itemsRevisit arrays in Module 8.
3 or more weak itemsRevisit functions and imports in Module 6.
02

The core idea

To plot, you build two arrays, x and the matching y, then call plt.plot(x, y). Always label both axes with units and add a title, so the picture is readable. A plot is not decoration: it is how you check that a result rises, falls, or curves the way physics says it should.

import matplotlib.pyplot as pltplt.plot(x, y)plt.xlabel(...); plt.ylabel(...)

Plotting is how engineers judge a result at a glance, and matplotlib is the standard tool. You import it once as import matplotlib.pyplot as plt. Every plot is built from two matching arrays: an x array of positions or times, and a y array of the values at those points, usually made by evaluating a formula, y = x ** 2, over an x built with np.linspace from Module 8. The call plt.plot(x, y) draws a line through the points; plt.scatter(x, y) draws separate markers, which suits measured data. What separates a useful plot from a useless one is labeling: plt.xlabel('position (m)'), plt.ylabel('stress (MPa)'), and plt.title(...) tell the reader, including future you, what the axes mean and in what units. An unlabeled plot is close to worthless in engineering. Finally, plt.show() displays it. The deeper point is that a plot is a check: if you expect stress to rise with load, the curve should rise; if a deflection should be small, the y-axis should show small numbers. Reading the shape of a curve against your physical expectation catches errors that a single printed number hides, which is why plotting belongs in every calculation you care about.

The skill works when: x and y line up, axes are labeled with units, and the shape matches your expectation.
The skill breaks down when: x and y have different lengths, or the plot is unlabeled and cannot be read.
The concept. Two matching arrays become a curve. Labeled axes and a title make it readable, and its shape is a check on the result.
03

The skills, taught in order

Five skills produce a clear, trustworthy plot.

9.1 Importing and the x-y pattern

import matplotlib.pyplot as plt. Build an x array (often np.linspace), compute the matching y, then plot the pair. x and y must have the same length.

9.2 Line and scatter plots

plt.plot(x, y) connects points with a line, good for a smooth function. plt.scatter(x, y) draws separate markers, good for measured data points.

9.3 Labeling

plt.xlabel('position (m)'), plt.ylabel('stress (MPa)'), and plt.title(...). Units in the labels are not optional in engineering; an unlabeled plot cannot be interpreted.

CallPurpose
plt.plot(x, y)line through the points
plt.scatter(x, y)markers for measured data
plt.xlabel / ylabel / titlelabel axes and name the plot
plt.show()display the figure

A complete plot is a handful of calls, but the labels are what make it useful.

9.4 Showing and saving

plt.show() displays the figure; plt.savefig('plot.png') writes it to a file for a report. Call these after the plot and labels are set.

9.5 Reading the plot as a check

Compare the curve to your expectation: does it rise, fall, or curve as the physics predicts? Are the y-values a sensible size? A plot that disagrees with intuition flags an error to chase.

Engineering connection: plotting a deflection along a beam or a temperature over time turns a column of numbers into a shape you can judge against theory in a second.

04

Worked example 1: plotting a parabola

Evaluate y = x squared at x = 0 to 5 and describe the plot.

import numpy as np
import matplotlib.pyplot as plt

x = np.array([0, 1, 2, 3, 4, 5])
y = x ** 2
print(y)

plt.plot(x, y)
plt.xlabel('x')
plt.ylabel('y = x squared')
plt.title('A parabola')
plt.show()

Printed values:

[ 0  1  4  9 16 25]
  1. ProblemPlot y = x squared and confirm its shape.
  2. Given / findx = 0,1,2,3,4,5. Find y and describe the curve.
  3. ModelVectorized y = x ** 2, then plt.plot(x, y) with labels.
  4. Solvey = [0, 1, 4, 9, 16, 25]; the curve rises ever more steeply.
  5. CheckThe gaps between values (1, 3, 5, 7, 9) grow, so the curve steepens, as a parabola should.
  6. ConclusionThe upward-curving shape confirms the squared relationship visually.
Result. y = [0, 1, 4, 9, 16, 25], a rising parabola.
05

Worked example 2: a straight line

Plot the line y = 2x + 1 at x = 0 to 3 and confirm it is straight.

import numpy as np
import matplotlib.pyplot as plt

x = np.array([0, 1, 2, 3])
y = 2 * x + 1
print(y)

plt.plot(x, y)
plt.xlabel('x')
plt.ylabel('y = 2x + 1')
plt.title('A straight line')
plt.show()

Printed values:

[1 3 5 7]
  1. ProblemPlot y = 2x + 1 and confirm it is a straight line.
  2. Given / findx = 0,1,2,3. Find y and describe the shape.
  3. ModelVectorized y = 2 * x + 1, then plot with labels.
  4. Solvey = [1, 3, 5, 7]; equal steps of 2, a straight line of slope 2.
  5. CheckEach step in x of 1 raises y by 2, and the intercept at x = 0 is 1, as the formula says.
  6. ConclusionA constant slope shows as a straight line, confirming the linear relationship.
Result. y = [1, 3, 5, 7], a straight line of slope 2.
06

Misconceptions and diagnostics

MistakeSymptomDiagnostic questionCorrection
x and y differ in lengthA dimension error"Do x and y match in size?"Build y from the same x array.
No axis labelsAn unreadable plot"Can a stranger read this?"Label both axes with units and add a title.
Missing plt.show()No figure appears"Did you call show?"End with plt.show() or savefig.
Ignoring the shapeA wrong result goes unnoticed"Does the curve match physics?"Read the shape as a check on the answer.
07

Practice ladder

Level 1 · Direct skill

Plot y = x for x from 0 to 10 and label both axes.

Show answer

Build x = np.linspace(0, 10, 11), set y = x, then plt.plot, label, and plt.show(). The result is a 45-degree line.

Level 2 · Mixed concept

Scatter-plot five measured data points and add a title.

Show answer

plt.scatter(x, y) with arrays of the five points, then plt.title(...), labels, and show.

Level 3 · Independent problem

Plot the deflection of a beam that varies as position cubed over its length, with labeled axes in metres and millimetres.

Show answer

Build x with linspace, compute y = k * x ** 3, and plot with xlabel('position (m)') and ylabel('deflection (mm)'). The curve should steepen toward the free end.

Transfer task | Real engineering

Take a real formula from another course, evaluate it over a sensible range, and produce a fully labeled plot you could put in a report.

What good work looks like

A curve over a physically sensible range, both axes labeled with units, a title, and a one-line note on whether the shape matches theory. A good answer treats the plot as a check, not decoration.

08

Working with AI, and proving it yourself

Use AI as a tutor, not a black box

"Explain when to use plot versus scatter."
"Give me a formula; I will predict the plot's shape, then draw it."
"Make all my plots." Build and label one plot yourself.
"Why is the figure blank?" Check for plt.show() yourself.

Portfolio task

Produce a labeled plot of an engineering relationship and write one sentence on whether its shape matches what theory predicts.

Must include: matching x and y arrays, labeled axes with units, a title, and a shape check.
09

Retrieval and spaced review

Closed notes. Answer out loud, then reveal.

1. How do you import matplotlib?

import matplotlib.pyplot as plt.

2. What two things does a plot need?

Matching x and y arrays of the same length.

3. Why label axes with units?

So the plot can be read and interpreted correctly.

4. Plot versus scatter?

Plot draws a connecting line; scatter draws separate markers.

5. How is a plot a check?

Its shape should match the physical behaviour you expect.

TodayFinish this quiz and Levels 1 and 2 of the ladder.
+1 dayMake a labeled plot from memory.
+3 daysPlot a formula from another course and check its shape.
+7 daysMove on to files and reproducibility in Module 10.
+30 daysLabel every plot with units by habit.
10

Textbook mapping

This module follows Hans Fangohr, Introduction to Python for Computational Science and Engineering. Use these references to read further.

Topic in this moduleWhere to read more
Plotting with matplotlibFangohr, chapter on Visualising data (matplotlib)
Line and scatter plots, labelsFangohr, Visualising data, plot basics
Building arrays to plotFangohr, Numerical Python, linspace and arrays

Chapter titles refer to Fangohr's Introduction to Python for Computational Science and Engineering. Any recent version is equivalent for study.