用imsh绘制hsv值

2024-06-28 19:07:07 发布

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

我试图用matplotlib中的imshow绘制hsv值。问题是我正在使用的方法返回一个元组,该元组的三个值与hsv的预期值相同,但是imshow将其解释为rgb。有没有办法告诉imshow值是hsv值?

这是我的代码:

import matplotlib.pyplot as plt
import numpy as np
import matplotlib.colors as mcolors


def G(x, y):
    s = x + 1j*y
    return (s+2)/(s**2 + s + 1)

x = np.linspace(-3, 3, 1000)
y = np.linspace(-3, 3, 1000)

xx, yy = np.meshgrid(x, y)
norm = mcolors.Normalize()
zz = G(xx, yy)
phase = np.angle(zz)
mag = np.abs(zz)

# color converter
c = mcolors.ColorConverter().to_rgb

# Custom rgb Colormap
rgb = make_colormap(
    [c('red'), c('yellow'), 0.33, c('yellow'), c('green'), c('cyan'), 0.5, c('cyan'),
     c('blue'), c('magenta'), 0.833, c('magenta'), c('red')])

# Turn data points into rgb values
z_data_rgb = rgb(norm(phase))
# normalizing the intensity values
intensity = norm(mag)

# defining light source
ls = mcolors.LightSource()

# plotting
plt.imshow(ls.blend_hsv(z_data_rgb, intensity), extent=[-3, 3, -3, 3])
plt.show()

我得到了下面的情节: enter image description here

如果它工作正常,根据强度值,图上的某些区域的饱和度应该低于其他区域。

谢谢


Tags: importnormdatamatplotlibasnppltrgb