How to build complex numbers (Argand diagram) using matplotlib

I would like to create an Argan diagram from a set of complex numbers using matplotlib.

Are there any built-in features to help me do this?

Can an approach be recommended?

+8
python numpy matplotlib plot complex-numbers
source share
2 answers

I donโ€™t know for sure that you are here ... do you have a set of complex numbers and you want to match them to a plane, using their real part as x coordinate and imaginary part as y?

If so, you can get the real part of the python imaginary number with number.real and the imaginary part with number.imag . If you use numpy, it also provides a set of helper functions numpy.real and numpy.imag, etc. that work with numpy arrays.

So, for example, if you had an array of complex numbers that stores 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 displays the points in the argand diagram for each point.

edit: for the part of the drawing, you should of course import matplotlib.pyplot via from matplotlib.pyplot import * or (like me) use the ipython shell in pylab mode.

+10
source share

Track @incential response; The following function creates an argand graph that centers around 0.0 and scales to the maximum absolute value in the set of complex numbers.

I used the graph function and set solid lines from (0,0). They can be removed by replacing ro- with ro .

 def argand(a): import matplotlib.pyplot as plt import numpy as np for x in range(len(a)): plt.plot([0,a[x].real],[0,a[x].imag],'ro-',label='python') limit=np.max(np.ceil(np.absolute(a))) # set limits for axis plt.xlim((-limit,limit)) plt.ylim((-limit,limit)) plt.ylabel('Imaginary') plt.xlabel('Real') plt.show() 

For example:

 >>> a = n.arange(5) + 1j*n.arange(6,11) >>> from argand import argand >>> argand(a) 

produces: argand function output graph

EDIT:

I just realized that there is a polar function:

 for x in a: plt.polar([0,angle(x)],[0,abs(x)],marker='o') 

enter image description here

+4
source share

All Articles