A4 · Lesson 32 · Plotting signals
A plot is how a calculation shows you it's right
A column of 2000 numbers tells you almost nothing. The same numbers drawn as a curve tell you at a glance whether a capacitor is charging, how fast, and whether you got a sign wrong somewhere. Engineers plot everything, early and often, and in Python that's matplotlib.
Predict
You run plt.plot(t, v) where t has 1000 values and v has 999. What happens?
An error, something like "x and y must have same first dimension". plt.plot(x, y) pairs the values up one to one, so both arrays need the same length. When this happens, it usually means one array was built with a different number of points, which is worth knowing about.
Name it
The few commands that cover most plots
import numpy as np
import matplotlib.pyplot as plt
t = np.linspace(0, 5e-3, 500)
v = 5 * np.exp(-t / 1e-3)
plt.plot(t * 1e3, v, label='v(t)') # x, y, and a name for the legend
plt.xlabel('t (ms)')
plt.ylabel('v (V)')
plt.axvline(1, linestyle='--') # a vertical line at t = 1 ms
plt.legend()
plt.grid(True)
plt.plot(x, y)draws a line through the points. Call it again to add more lines to the same axes.- Labels with units on both axes, always. Scaling the data (
t * 1e3) and labeling the axismsis cleaner than an axis of tiny numbers. label=andplt.legend()name the lines.plt.axvline(x)andplt.axhline(y)draw reference lines, such as a time constant or a threshold.- Log axes:
plt.semilogx,plt.semilogyandplt.loglogwork likeplt.plotwith one or both axes logarithmic, for Bode plots and anything spanning decades. plt.subplot(2, 1, 1)starts the first of two stacked panels;plt.subplot(2, 1, 2)the second.
In these labs the figure appears under your code when it runs.
Worked example
Plot a voltage and a current on the same axes and tell them apart.
Show the solution
- Scale the current to a readable size. Plotting puts both on a readable scale.
- Label each line. The label says the current is scaled, the way a scope's current probe setting does.
import numpy as np
import matplotlib.pyplot as plt
t = np.linspace(0, 0.04, 800)
v = 10 * np.cos(2 * np.pi * 50 * t)
i = 2 * np.cos(2 * np.pi * 50 * t - np.pi / 3)
plt.plot(t * 1e3, v, label="v (V)")
plt.plot(t * 1e3, 5 * i, label="5 × i (A)")
plt.xlabel("t (ms)")
plt.legend()
- Read the plot. The current visibly lags by 60°.
Answer plot and on the same axes with labels and a legend; the current lags the voltage by 60°.
Worked example
Mark the time constant on a discharge curve.
Show the solution
- Plot the discharge. 12 V decaying with = 4.7 ms, out to .
- Add the reference lines. A dashed vertical line at and a dotted horizontal line at .
tau = 4.7
t = np.linspace(0, 5 * tau, 300)
plt.plot(t, 12 * np.exp(-t / tau))
plt.axvline(tau, linestyle="--", color="grey")
plt.axhline(12 * np.exp(-1), linestyle=":", color="grey")
plt.xlabel("t (ms)"); plt.ylabel("v (V)")
- Read the crossing. The two reference lines cross on the curve at , the defining point of a time constant.
Answer mark the time constant with axvline at and axhline at V; they meet on the curve.
Check
Plot a capacitor's discharge, with = 1 ms, from 0 to , with time in milliseconds on the x axis. Label both axes and mark with a dashed vertical line.
V0 = 6 (volts) and tau = 1 (milliseconds) are already defined.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
On one set of axes, plot and over 40 ms, with time in ms, and add a legend naming them v1 and v2.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Which call draws a Bode magnitude plot, with frequency on a log axis and decibels on a linear one?
Check
The starter has ten measured points, voltages V and currents I in amps, from a resistor of about 2200 Ω. Plot the points as dots with no line joining them ('o'), then the straight line through them as a solid line over the same voltages. Label both axes with units.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Plot the output of a bits-bit ADC (3 bits, so codes 0 to 2**bits - 1, full scale 1 V) for an input ramp vin from 0 to 1 V over 1 ms, as a staircase: use plt.step(t_ms, code, where='post'), where code is np.floor(vin * 2**bits) clipped to the top code. Label both axes.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
You want the y axis to run from −1 to 1 whatever the data is. Which call does it?
Explain why
Why do engineers scale data before plotting it, for example plotting t * 1e3 against an axis labeled "t (ms)"?
An axis reading 0, 0.001, 0.002 is hard to read and easy to misread by a factor of a thousand. Scaling the numbers to a sensible prefix and saying so in the label gives tick marks like 0, 1, 2 ms that you can read at a glance and compare directly with datasheets and scopes, which use the same prefixes. The data are the same; only the units they're shown in change.
Practice
Keep practicing
6 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.