如何使用matplotlib绘制复数(Argand图)

问题描述 投票:14回答:4

我想使用matplotlib从一组复数创建一个Argand Diagram

  • 有没有内置的函数可以帮助我做到这一点?

  • 任何人都可以推荐一种方法吗?

enter image description here

[Image by LeonardoG,CC-SA-3.0

python numpy matplotlib plot complex-numbers
4个回答
16
投票

我不确定您在这里究竟要什么...您有一组复数,想通过将它们的实部用作x坐标并将虚部用作y来将它们映射到平面?

如果是这样,您可以使用number.real获得任何python虚数的实部,并使用number.imag获得虚部。如果您使用的是numpy,它还会提供一组辅助函数numpy.real和numpy.imag等,它们可以在numpy数组上使用。

例如,如果您有一个复数数组存储了这样的内容:

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])

...您可以做

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

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

这将在每个点的argand图上绘制点。

编辑:对于绘图部分,您当然必须通过from matplotlib.pyplot import *导入了matplotlib.pyplot,或者(如我所做的那样)在pylab模式下使用ipython shell。


7
投票

跟随@inclement的回答;以下函数将生成一个以0,0为中心并缩放为复数集中的最大绝对值的argand图。

我使用了绘图功能,并从(0,0)指定了实线。可以通过将ro-替换为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()

例如:

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

产生:“参数功能输出图”“>

编辑:

我刚刚意识到也有一个polar绘图功能:

polar

“在此处输入图像描述”“ >>

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

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()

或此one type of plot

您可以通过这两行简单地完成此操作(以上面的图为例):

z = [20 + 10j,15,-10-10j,5 + 15j]#复数值数组

complex_plane2(z,1)#要调用的函数

通过从此处使用简单的jupyter代码second type of plot

我出于个人目的编写了它。更好的是它对其他人有帮助。


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

0
投票

如果您喜欢下面的情节

© www.soinside.com 2019 - 2024. All rights reserved.