Plotting hsv values with imshow

Neill Herbst picture Neill Herbst · May 30, 2016 · Viewed 9k times · Source

I'm trying to plot hsv values using imshow in matplotlib. The problem is the method I'm using returns a tuple with three values as expected for hsv but imshow interpretes this as rgb. Is there a way of telling imshow that the values are hsv values?

Here is my code:

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

I get the following plot: enter image description here

If it worked correctly some areas on the plot should have less saturation than others based on the intensity values.

Thanks

Answer

Serenity picture Serenity · May 30, 2016

Why not use hsv_to_rgb and plot with rgb colors?

from matplotlib.colors import hsv_to_rgb
rgb = hsv_to_rgb(hsv)