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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17445720/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 08:14:54  来源:igfitidea点击:

How to plot complex numbers (Argand Diagram) using matplotlib

pythonnumpymatplotlibplotcomplex-numbers

提问by atomh33ls

I'd like to create an Argand Diagramfrom a set of complex numbers using matplotlib.

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

  • Are there any pre-built functions to help me do this?

  • Can anyone recommend an approach?

  • 是否有任何预先构建的功能可以帮助我做到这一点?

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

enter image description here

在此处输入图片说明

Imageby LeonardoG, CC-SA-3.0

图片LeonardoG 提供,CC-SA-3.0

采纳答案by inclement

I'm not sure exactly what you're after here...you have a set of complex numbers, and want to map them to the plane by using their real part as the x coordinate and the imaginary part as y?

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

If so you can get the real part of any python imaginary number with number.realand the imaginary part with number.imag. If you're using numpy, it also provides a set of helper functions numpy.real and numpy.imag etc. which work on numpy arrays.

如果是这样,你可以得到任何 python 虚数的实部number.real和虚部number.imag。如果您使用 numpy,它还提供了一组辅助函数 numpy.real 和 numpy.imag 等,它们适用于 numpy 数组。

So for instance if you had an array of complex numbers stored something like this:

因此,例如,如果您有一个复数数组,存储如下:

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

...you can just do

...你可以这样做

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

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

This plots dots on an argand diagram for each point.

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

edit: For the plotting part, you must of course have imported matplotlib.pyplot via from matplotlib.pyplot import *or (as I did) use the ipython shell in pylab mode.

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

回答by atomh33ls

To follow up @inclement's answer; the following function produces an argand plot that is centred around 0,0 and scaled to the maximum absolute value in the set of complex numbers.

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

I used the plot function and specified solid lines from (0,0). These can be removed by replacing ro-with ro.

我使用了 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()

For example:

例如:

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

produces: argand function output graph

产生: argand 函数输出图

EDIT:

编辑:

I have just realised there is also a polarplot function:

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

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

enter image description here

在此处输入图片说明

回答by Holomorphic Guy

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

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

回答by Dejan

If you prefer a plot like the one below

如果你喜欢像下面这样的情节

one type of plot

一种情节

or this one second type of plot

或者这第二种类型的情节

you can do this simply by these two lines (as an example for the plots above):

你可以简单地通过这两行来做到这一点(作为上图的例子):

z=[20+10j,15,-10-10j,5+15j] # array of complex values

complex_plane2(z,1) # function to be called

by using a simple jupyter code from here https://github.com/osnove/other/blob/master/complex_plane.py

通过从这里使用一个简单的 jupyter 代码 https://github.com/osnove/other/blob/master/complex_plane.py

I have written it for my own purposes. Even better it it helps to others.

我写它是为了我自己的目的。更好的是它可以帮助他人。