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

2024-05-18 07:33:24 发布

您现在位置:Python中文网/ 问答频道 /正文

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

  • 是否有任何预先构建的函数可以帮助我执行此操作?

  • 有人能推荐一种方法吗?

enter image description here

ImageLeonardoG,CC-SA-3.0


Tags: 方法函数imagematplotlibsadiagramcc复数
3条回答
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()

https://github.com/QuantumNovice/Matplotlib-Argand-Diagram/blob/master/argand.py

跟踪@increment的答案;下面的函数生成一个以0,0为中心的argand图,并缩放到复数集中的最大绝对值。

我使用plot函数并指定了(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)

产生: argand function output graph

编辑:

我刚刚意识到还有一个^{}绘图函数:

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

enter image description here

我不确定你到底在找什么…你有一组复数,想用它们的实部作为x坐标,虚部作为y,把它们映射到平面上?

如果是这样的话,可以用number.real得到任何python虚数的实部,用number.imag得到虚部。如果您使用的是numpy,它还提供了一组在numpy数组上工作的助手函数numpy.real和numpy.imag等。

例如,如果你有一个复数数组,存储如下:

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。

相关问题 更多 >