matplotlib imshow 复杂的二维数组 [英] matplotlib imshow complex 2D array

查看:46
本文介绍了matplotlib imshow 复杂的二维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么好的方法可以在 mathplotlib 中将复数的二维数组绘制为图像吗?

Is there any good way how to plot 2D array of complex numbers as image in mathplotlib ?

将复数的幅度映射为亮度"或饱和度"并将相位映射为色调"非常有意义(无论如何,色调只不过是 RBG 颜色空间中的相位). http://en.wikipedia.org/wiki/HSL_and_HSV

It makes very much sense to map magnitude of complex number as "brightness" or "saturation" and phase as "Hue" ( anyway Hue is nothing else than phase in RBG color space). http://en.wikipedia.org/wiki/HSL_and_HSV

但据我所知 imshow 只接受标量值,然后使用一些色标进行映射.有没有像绘制真实的RGB图片一样的东西?

But as far as I know imshow does accept only scalar values which are then mapped using some colorscale. There is nothing like ploting real RGB pictures?

我认为只要实现一个接受 3 个浮点数的元组(向量)的二维数组或形状为 [:,:,3] 的浮点数的 ndarray 的版本就很容易了.我想这通常是有用的功能.它也可用于绘制真实的 RGB 彩色图像,例如从 OpenCL 输出的纹理

I thing it would be easy just implement a version which accepts 2D array of tuples (vectors) of 3 floating point numbers or ndarray of floats of shape [:,:,3]. I guess this would be generally usefful feature. It would be also usefull for plotting real RGB colord images, such as textures outputted from OpenCL

推荐答案

这几乎与@Hooked代码相同,但速度更快.

this does almost the same of @Hooked code but very much faster.

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

这篇关于matplotlib imshow 复杂的二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆