import matplotlib.pyplot as plt
from numpy import *
'''
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`
This draws the axis for argand diagram
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`
'''
r = 1
Y = [r*exp(1j*theta) for theta in linspace(0,2*pi, 200)]
Y = array(Y)
plt.plot(real(Y), imag(Y), 'r')
plt.ylabel('Imaginary')
plt.xlabel('Real')
plt.axhline(y=0,color='black')
plt.axvline(x=0, color='black')
def argand(complex_number):
'''
This function takes a complex number.
'''
y = complex_number
x1,y1 = [0,real(y)], [0, imag(y)]
x2,y2 = [real(y), real(y)], [0, imag(y)]
plt.plot(x1,y1, 'r') # Draw the hypotenuse
plt.plot(x2,y2, 'r') # Draw the projection on real-axis
plt.plot(real(y), imag(y), 'bo')
[argand(r*exp(1j*theta)) for theta in linspace(0,2*pi,100)]
plt.show()
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()
例如:
>>> a = n.arange(5) + 1j*n.arange(6,11)
>>> from argand import argand
>>> argand(a)
https://github.com/QuantumNovice/Matplotlib-Argand-Diagram/blob/master/argand.py
跟踪@increment的答案;下面的函数生成一个以0,0为中心的argand图,并缩放到复数集中的最大绝对值。
我使用plot函数并指定了(0,0)中的实线。这些可以通过用
ro
替换ro-
来删除。例如:
产生:
编辑:
我刚刚意识到还有一个^{} 绘图函数:
我不确定你到底在找什么…你有一组复数,想用它们的实部作为x坐标,虚部作为y,把它们映射到平面上?
如果是这样的话,可以用
number.real
得到任何python虚数的实部,用number.imag
得到虚部。如果您使用的是numpy,它还提供了一组在numpy数组上工作的助手函数numpy.real和numpy.imag等。例如,如果你有一个复数数组,存储如下:
……你可以
这在argand图上为每个点绘制点。
编辑:对于绘图部分,您当然必须通过
from matplotlib.pyplot import *
导入matplotlib.pyplot,或者(正如我所做的那样)在pylab模式下使用ipython shell。相关问题 更多 >
编程相关推荐