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:
If it worked correctly some areas on the plot should have less saturation than others based on the intensity values.
Thanks
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).
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() .
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.
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.
Why not use hsv_to_rgb
and plot with rgb
colors?
from matplotlib.colors import hsv_to_rgb
rgb = hsv_to_rgb(hsv)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With