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.
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.
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.
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 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.
| Call | Purpose |
|---|---|
plt.plot(x, y) | line through the points |
plt.scatter(x, y) | markers for measured data |
plt.xlabel / ylabel / title | label 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.
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]
- ProblemPlot y = x squared and confirm its shape.
- Given / findx = 0,1,2,3,4,5. Find y and describe the curve.
- ModelVectorized
y = x ** 2, thenplt.plot(x, y)with labels. - Solvey = [0, 1, 4, 9, 16, 25]; the curve rises ever more steeply.
- CheckThe gaps between values (1, 3, 5, 7, 9) grow, so the curve steepens, as a parabola should.
- ConclusionThe upward-curving shape confirms the squared relationship visually.
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]
- ProblemPlot y = 2x + 1 and confirm it is a straight line.
- Given / findx = 0,1,2,3. Find y and describe the shape.
- ModelVectorized
y = 2 * x + 1, then plot with labels. - Solvey = [1, 3, 5, 7]; equal steps of 2, a straight line of slope 2.
- CheckEach step in x of 1 raises y by 2, and the intercept at x = 0 is 1, as the formula says.
- ConclusionA constant slope shows as a straight line, confirming the linear relationship.
Misconceptions and diagnostics
| Mistake | Symptom | Diagnostic question | Correction |
|---|---|---|---|
| x and y differ in length | A dimension error | "Do x and y match in size?" | Build y from the same x array. |
| No axis labels | An 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 shape | A wrong result goes unnoticed | "Does the curve match physics?" | Read the shape as a check on the answer. |
Practice ladder
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.
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.
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.
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.
Working with AI, and proving it yourself
Use AI as a tutor, not a black box
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.
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.
Textbook mapping
This module follows Hans Fangohr, Introduction to Python for Computational Science and Engineering. Use these references to read further.
| Topic in this module | Where to read more |
|---|---|
| Plotting with matplotlib | Fangohr, chapter on Visualising data (matplotlib) |
| Line and scatter plots, labels | Fangohr, Visualising data, plot basics |
| Building arrays to plot | Fangohr, 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.