Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotting hsv values with imshow

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

like image 899
Neill Herbst Avatar asked May 30 '16 07:05

Neill Herbst


People also ask

Does Imshow normalize image?

By default, imshow normalizes the data to its min and max. You can control this with either the vmin and vmax arguments or with the norm argument (if you want a non-linear scaling).

What is plot Imshow?

imshow. The matplotlib function imshow() creates an image from a 2-dimensional numpy array. The image will have one square for each element of the array. The color of each square is determined by the value of the corresponding array element and the color map used by imshow() .

How do you scale Imshow?

Use the extent parameter of imshow to map the image buffer pixel coordinates to a data space coordinate system. Next, set the aspect ratio of the image manually by supplying a value such as "aspect=4" or let it auto-scale by using aspect='auto'. This will prevent stretching of the image.

What is Imshow return?

As you have already found out, the return type of plt. imshow() is a matplotlib. image. AxesImage . The object img you get when calling img = plt.


1 Answers

Why not use hsv_to_rgb and plot with rgb colors?

from matplotlib.colors import hsv_to_rgb
rgb = hsv_to_rgb(hsv)
like image 64
Serenity Avatar answered Sep 19 '22 10:09

Serenity