How to plot complex numbers (Argand Diagram) using matplotlib

atomh33ls picture atomh33ls · Jul 3, 2013 · Viewed 60k times · Source

I'd like to create an Argand Diagram from a set of complex numbers using matplotlib.

  • Are there any pre-built functions to help me do this?

  • Can anyone recommend an approach?

enter image description here

Image by LeonardoG, CC-SA-3.0

Answer

inclement picture inclement · Jul 3, 2013

I'm not sure exactly what you're after here...you have a set of complex numbers, and want to map them to the plane by using their real part as the x coordinate and the imaginary part as y?

If so you can get the real part of any python imaginary number with number.real and the imaginary part with number.imag. If you're using numpy, it also provides a set of helper functions numpy.real and numpy.imag etc. which work on numpy arrays.

So for instance if you had an array of complex numbers stored something like this:

In [13]: a = n.arange(5) + 1j*n.arange(6,11)

In [14]: a
Out[14]: array([ 0. +6.j,  1. +7.j,  2. +8.j,  3. +9.j,  4.+10.j])

...you can just do

In [15]: fig,ax = subplots()

In [16]: ax.scatter(a.real,a.imag)

This plots dots on an argand diagram for each point.

edit: For the plotting part, you must of course have imported matplotlib.pyplot via from matplotlib.pyplot import * or (as I did) use the ipython shell in pylab mode.