Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to plot complex number

Tags:

python

numbers

I am trying to plot a point in the complex number plane like that:

from matplotlib.pyplot import*
i = complex(3,1)*2
plot(i,'b.')
show()`

However the resulting plot displays the point z=3+0i. How can I make it take the imaginary component into account?

like image 708
mouy Avatar asked Sep 17 '26 03:09

mouy


2 Answers

Casting complex values to real discards the imaginary part.

Please try this

%matplotlib inline
import matplotlib.pyplot as plt
i = complex(3,1)*2
plt.plot(i.real, i.imag,'b.')
plt.show()
like image 69
Werner Avatar answered Sep 19 '26 17:09

Werner


You need to access the real and imaginary parts of your object i:

plot(i.real,i.imag,'b.')

Edit:

To adress the question you are asking in the comments, you just supply the list of real parts and imaginary parts independently, and you also have to remove the "." from the "b." part, e.g.

# a and b are instances of complex
plt.plot([a.real, b.real], [a.imag, b.imag],"b")

Note that you can find this information using the help() function on those functions.

like image 24
Ash Avatar answered Sep 19 '26 18:09

Ash