I created a function that is a wrap around matplotlib imshow that basically add a proper colorbar when I show a RGBA image (for which the cmap parameter is ignored) This is how I create the colormap
import matplotlib.colors as mcol
from matplotlib import cm
start_hue, stop_hue = 0, 360 # this is used to evnt. select only a subset of the available colors in the HSV colorspace. Specific values are not particularly relevant for my question
vmin, vmax = 0, np.pi/2 # example
color_range = np.arange(start_hue, stop_hue) / 360
hsv_cmap = cm.get_cmap('hsv')
cmap = mcol.ListedColormap(hsv_cmap(color_range))
cbar_mappable= cm.ScalarMappable(cmap=cmap)
cmap.set_clim(vmin=vmin, vmax=vmax)
Then I add the colorbar to a Figure instance containing my imshow plot
cax = plt.axes([0.87, 0.1, 0.025, 0.8])
fig.colorbar(cbar_mappable, cax=cax)
Everything looks fine. When I plot the image with my function the colorbar shows the right colors and the correct vmin, vmax values I set for it. However,I tried the same code on a different computer and I got the error:
TypeError: You must first set_array for mappable
So to solve this I simply do:
cbar_mappable.set_array(my_rgba_nparray)
Questions
- is my approach correct? is it ok to use the RGBA array for set_array() ?
- why do I need to use set_array()? Isn't it already set when I do
cmap = mcol.ListedColormap(hsv_cmap(color_range)) ?
- why is on one computer working just fine even if I DO NOT use set_array()? What should I check to find out why there is this different behavior on different machines?