Skip to content
Tutorials Center
My learning

Topics

Lesson 4 of 36 ยท Getting Started

Plotting Complex Numbers with Python

Set up your Python toolkit for complex analysis: the complex type, cmath, NumPy arrays, and matplotlib plots of points, vectors and maps on the complex plane.

Complex analysis is a visual subject, and a few lines of Python can turn abstract formulas into pictures. This lesson gathers the tools you will use through the rest of the track: Python's complex type, the cmath module, NumPy arrays of complex numbers, and matplotlib for drawing on the complex plane.

The tools

Tool Use
complex(a, b) or a + bj A single complex number
z.real, z.imag, abs(z), z.conjugate() Parts, modulus, conjugate
cmath exp, log, sqrt, sin, phase, polar for single numbers
numpy The same functions on whole arrays at once (np.exp(z))
matplotlib Plot z.real on the x-axis and z.imag on the y-axis

Mixing the two libraries is the usual mistake: cmath functions accept one number, NumPy functions accept arrays. For a single number either works, but only NumPy will vectorize.

Python Python's complex toolbox

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The phase of 3 - 4j is negative because the point lies below the real axis (in the fourth quadrant). cmath.polar returns the modulus and the angle in one call.

Drawing numbers as points and arrows

A complex number z = x + iy is drawn at the point (x, y). It is often helpful to draw it as an arrow from the origin. The next program plots three numbers, their sum and their conjugates.

Python Points and vectors on the plane

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice ax.set_aspect("equal"). Without it a circle looks like an ellipse and angles are distorted, which defeats the purpose of drawing the complex plane. Always set equal aspect.

Plotting a whole set at once

Because NumPy vectorizes, you can generate hundreds of points with one expression. For example, the unit circle is exp(i t) for t from 0 to 2ฯ€ (we justify this in the lesson on Euler's formula). Here is a small helper you can reuse: it plots any list of complex numbers.

Python A reusable plotting helper

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

You just saw two basic geometric facts in action. Multiplying by a positive real number scales the picture about the origin, and adding a complex number translates it. Later you will see that multiplying by a complex number also rotates it.

Practice habits

When you meet a new formula, test it on a few points with code before trusting it. If the code and your hand calculation disagree, the difference tells you where to look.

  • Prefer abs(z) and np.angle(z) over writing sqrt(x**2 + y**2) yourself.
  • Use np.round(z, 4) when printing, because floating-point results such as 1.0000000000000002 are noise.
  • Compare floats with np.isclose, never with ==.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which axis shows the imaginary part of z in the complex plane?

    The real part is plotted on the horizontal axis and the imaginary part on the vertical axis.
  2. 2. Why call ax.set_aspect('equal') when plotting the complex plane?

    Equal aspect gives the same scale on both axes, which preserves the geometry of the plane.
  3. 3. Which function should you use to apply exp to a whole NumPy array of complex numbers?

    np.exp is vectorized and accepts complex arrays; cmath.exp accepts a single number and math.exp rejects complex input.
  4. 4. What does adding the complex number 1 + 0.5i to every point of a set do to its picture?

    Adding a fixed complex number adds the same vector to every point, which is a translation.

Summary

  • Python offers complex, cmath (single numbers), NumPy (arrays) and matplotlib (pictures).
  • A number x + iy is plotted at (x, y); use equal aspect ratio.
  • Addition follows the parallelogram rule; real scaling stretches; adding a constant translates.
  • Print rounded results and compare floats with np.isclose.

Next: The Complex Plane and Modulus