A432 of 56
0 XP
  1. Hook
  2. Predict
  3. Name it
  4. Check
  5. Explain

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?

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 axis ms is cleaner than an axis of tiny numbers.
  • label= and plt.legend() name the lines.
  • plt.axvline(x) and plt.axhline(y) draw reference lines, such as a time constant or a threshold.
  • Log axes: plt.semilogx, plt.semilogy and plt.loglog work like plt.plot with 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
  1. Scale the current to a readable size. Plotting 5×i puts both on a readable scale.
  2. 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()
  1. Read the plot. The current visibly lags by 60°.

Answer plot v and 5×i 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
  1. Plot the discharge. 12 V decaying with τ = 4.7 ms, out to 5τ.
  2. Add the reference lines. A dashed vertical line at t=τ and a dotted horizontal line at 12e−1.
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)")
  1. Read the crossing. The two reference lines cross on the curve at (τ,0.368×12), the defining point of a time constant.

Answer mark the time constant with axvline at τ and axhline at 0.368×12 V; they meet on the curve.

Check

Plot a capacitor's discharge, v=6e−t/τ with τ = 1 ms, from 0 to 5τ, with time in milliseconds on the x axis. Label both axes and mark t=τ 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 2cos⁡(2π⋅50t) and 2cos⁡(2π⋅50t−30°) 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 I=V/R 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)"?

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.