我想制作一个色相强度图。即一幅图像显示为彩色(色调),另一幅显示亮度(强度)。我有一个方法,但是我想用颜色条来量化图。一种用于色相数据,另一种用于强度数据。
我尝试使用fig.add_axes
方法添加颜色条。但是,这会导致颜色条不适合图中的颜色,因此需要在位置上四处移动。当尝试显示不同尺寸的图像时,这尤其令人沮丧,因为色带相对于图像位于不同的位置。
from matplotlib import colors, cm, pyplot as plt
import matplotlib
import numpy as np
# Generate some random data
intensity = np.random.rand(10, 10)
colour = np.random.rand(10, 10)
norm_c = colors.Normalize(vmin=colour.min(), vmax=colour.max()) # Set hue range
cmap_c = cm.coolwarm # Choose colormap for hues
m = cm.ScalarMappable(norm=norm_c, cmap=cmap_c)
c_rgb = m.to_rgba(colour) # get RGB values
norm_i = matplotlib.colors.Normalize(vmin=intensity.min(), vmax=intensity.max()) # Set itensity range
cmap_i = cm.gray # Leave this colormap - needs to be gray to show itensity
m = cm.ScalarMappable(norm=norm_i, cmap=cmap_i)
i_rgb = m.to_rgba(intensity) # Get RGB values
# Convert to HSV
c_hsv = colors.rgb_to_hsv(c_rgb[:,:,:-1])
i_hsv = colors.rgb_to_hsv(i_rgb[:,:,:-1])
# Use colour as hue and intensity as value
tot_hsv = np.copy(c_hsv)
tot_hsv[:,:,-1] = i_hsv[:,:,-1]
tot_rgb = colors.hsv_to_rgb(tot_hsv) # Convert back to RGB
fig = plt.figure()
ax = fig.add_subplot(111) # Have to use this method for use with another package (astropy projection)
im = ax.imshow(tot_rgb, origin='lower') # Show hue-intensity image
# Attempt at colorbars
cax1 = fig.add_axes([0.8,0.11,0.05,0.77])
cb2 = matplotlib.colorbar.ColorbarBase(cax1, cmap=cmap_c, norm=norm_c, orientation='vertical')
cb2.set_label('Colour')
cax2 = fig.add_axes([0.9,0.11,0.05,0.77])
cb2 = matplotlib.colorbar.ColorbarBase(cax2, cmap=cmap_i, norm=norm_i, orientation='vertical')
cb2.set_label('Intensity')
这会导致颜色条偏离图像的侧面(或顶部)。我真的很想让彩条更自动化地放置。