如何用matplotlib创建二维复数阵列的相图?

z4iuyo4d  于 2023-03-03  发布在  其他
关注(0)|答案(5)|浏览(142)

有没有什么好的方法可以在mathplotlib中将复数的二维数组绘制成图像?
将复数的幅值Map为“亮度”或“饱和度”以及将相位Map为“色调”是非常有意义的(无论如何,色调就是RBG颜色空间中的相位)。http://en.wikipedia.org/wiki/HSL_and_HSV
但据我所知imshow只接受标量值,然后使用一些色阶Map。没有什么比绘制真实的的RGB图片更好的了。
我想它会很容易实现一个版本,它接受3个浮点数的元组(向量)的2D数组或形状[:,:,3]的浮点数的ndarray。我想这通常是有用的功能。它也会对绘制真实的RGB颜色图像有用,如从OpenCL输出的纹理

zujrkrfu

zujrkrfu1#

这几乎和@Hooked代码一样,但是要快得多。

import numpy as np
from numpy import pi
import pylab as plt
from colorsys import hls_to_rgb

def colorize(z):
    r = np.abs(z)
    arg = np.angle(z) 

    h = (arg + pi)  / (2 * pi) + 0.5
    l = 1.0 - 1.0/(1.0 + r**0.3)
    s = 0.8

    c = np.vectorize(hls_to_rgb) (h,l,s) # --> tuple
    c = np.array(c)  # -->  array of (3,n,m) shape, but need (n,m,3)
    c = c.swapaxes(0,2) 
    return c

N=1000
x,y = np.ogrid[-5:5:N*1j, -5:5:N*1j]
z = x + 1j*y

w = 1/(z+1j)**2 + 1/(z-2)**2
img = colorize(w)
plt.imshow(img)
plt.show()
qhhrdooz

qhhrdooz2#

修改mpmath的绘图代码,即使你不知道numpy和matplotlib的原始函数**,你也可以用它来绘制numpy数组**,如果你知道这个函数,see my original answer可以使用mpmath.cplot

from colorsys import hls_to_rgb

def colorize(z):
    n,m = z.shape
    c = np.zeros((n,m,3))
    c[np.isinf(z)] = (1.0, 1.0, 1.0)
    c[np.isnan(z)] = (0.5, 0.5, 0.5)

    idx = ~(np.isinf(z) + np.isnan(z))
    A = (np.angle(z[idx]) + np.pi) / (2*np.pi)
    A = (A + 0.5) % 1.0
    B = 1.0 - 1.0/(1.0+abs(z[idx])**0.3)
    c[idx] = [hls_to_rgb(a, b, 0.8) for a,b in zip(A,B)]
    return c

从这里,你可以绘制一个任意的复数numpy数组:

N = 1000
A = np.zeros((N,N),dtype='complex')
axis_x = np.linspace(-5,5,N)
axis_y = np.linspace(-5,5,N)
X,Y = np.meshgrid(axis_x,axis_y)
Z = X + Y*1j

A = 1/(Z+1j)**2 + 1/(Z-2)**2

# Plot the array "A" using colorize
import pylab as plt
plt.imshow(colorize(A), interpolation='none',extent=(-5,5,-5,5))
plt.show()

xtfmy6hx

xtfmy6hx3#

mpmath使用matplotlib生成复杂平面的美丽图像。在复杂平面上,您通常关心极点,因此函数的参数给出颜色(因此极点将形成螺旋)。极大或极小值的区域由饱和度控制。来自文档:
默认情况下,复数参数(相位)显示为颜色(色调),幅值显示为亮度。您还可以提供自定义颜色函数(* color *)。此函数应将复数作为输入,并返回包含范围为0.0 - 1.0的浮点数的RGB三元组。
示例:

import mpmath
mpmath.cplot(mpmath.gamma, points=100000)

显示zeta function、平凡零点和临界条带的另一个示例:

import mpmath
mpmath.cplot(mpmath.zeta, [-45,5],[-25,25], points=100000)

2q5ifsrm

2q5ifsrm4#

您可以使用matplotlib.colors.hsv_to_rgb代替colorsys.hls_to_rgbmatplotlib函数大约快10倍!请查看以下结果:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import hsv_to_rgb
import time

def Complex2HSV(z, rmin, rmax, hue_start=90):
    # get amplidude of z and limit to [rmin, rmax]
    amp = np.abs(z)
    amp = np.where(amp < rmin, rmin, amp)
    amp = np.where(amp > rmax, rmax, amp)
    ph = np.angle(z, deg=1) + hue_start
    # HSV are values in range [0,1]
    h = (ph % 360) / 360
    s = 0.85 * np.ones_like(h)
    v = (amp -rmin) / (rmax - rmin)
    return hsv_to_rgb(np.dstack((h,s,v)))

下面是@nadapez挑选答案的方法:

from colorsys import hls_to_rgb
def colorize(z):
    r = np.abs(z)
    arg = np.angle(z) 

    h = (arg + np.pi)  / (2 * np.pi) + 0.5
    l = 1.0 - 1.0/(1.0 + r**0.3)
    s = 0.8

    c = np.vectorize(hls_to_rgb) (h,l,s) # --> tuple
    c = np.array(c)  # -->  array of (3,n,m) shape, but need (n,m,3)
    c = c.swapaxes(0,2) 
    return c

用1024*1024二维阵列对两种方法的结果进行检验:

N=1024
x, y = np.ogrid[-4:4:N*1j, -4:4:N*1j]
z = x + 1j*y

t0 = time.time()
img = Complex2HSV(z, 0, 4)
t1 = time.time()
print "Complex2HSV method: "+ str (t1 - t0) +" s"

t0 = time.time()
img = colorize(z)
t1 = time.time()
print "colorize method: "+ str (t1 - t0) +" s"

在我的旧笔记本电脑上显示的结果是:

Complex2HSV method: 0.250999927521 s
colorize method: 2.03200006485 s
piv4azn7

piv4azn75#

您也可以使用PIL.image来转换它

import PIL.Image

def colorize(z):
  z = Zxx
  n,m = z.shape

  A = (np.angle(z) + np.pi) / (2*np.pi)
  A = (A + 0.5) % 1.0 * 255
  B = 1.0 - 1.0/(1.0+abs(z)**0.3)
  B = abs(z)/ z.max() * 255
  H = np.ones_like(B)
  image = PIL.Image.fromarray(np.stack((A, B, np.full_like(A, 255)), axis=-1).astype(np.uint8), "HSV") # HSV has range 0..255 for all channels
  image = image.convert(mode="RGB")

  return np.array(image)

相关问题