开发者

Plotting a list of Complex nos on Z-plane in python

I tried:

plot(z)

where z is a list of complex numbers, which plots abs(z) versus index.

plot( z.r开发者_如何学编程eal, z.imag )

doesn't work, it says list doesn't have attribute real.


If z is a list of complex, use

[k.real for k in z]

to extract the real parts of every number in the list.


If I'm understanding your question correctly, it might work if you fix the attribute error. ".real" and ".imag" must be performed on complex numbers as far as I know, meaning they won't work unless there is a j component:

>>> a = 2.5
>>> print a.real
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'float' object has no attribute 'real'
>>> a = 2.5 + 0j
>>> print a.real
2.5

It looks to me like in your arguments for plot(), you tried to use ".real" and ".imag" on a list. I'd suggest trying to use ".real" and ".imag" on the complex numbers inside the list themselves. For a list of complex numbers z:

>>> z[0].real
>>> z[0].imag

will call the real and imaginary parts of the first complex number in z, respectively. There are many ways to do this I'm sure, but the following is pretty easy:

>>> x = []
>>> y = []
>>> for num in z:
...     x.append(num.real)
...     y.append(num.imag)
...
>>> plot(x,y)

Sorry that was so wordy, I'm really tired lol. While I've never used plot() before, my understanding is that it can plot lists so that should work.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜