Roman
Roman

Reputation: 1466

Separate colormap for each subplot in Octave

Matlab provides an ability to set individual colormaps for each subplot. The same functionality described by Octave docs

Namely following phrases:

Function File: cmap = colormap (hax, …) If the first argument hax is an axes handle, then the colormap for the parent figure of hax is queried or set.

But when I try to run following code, I end up having single colormap for all subplots for some reason.

clear -all;
clc;

img = zeros(128, 128);
img(64,64) = 0.2;
rad = radon(img);

x = 1;
y = 4;


s1 = subplot(y,x,1); imagesc(img);
colormap(s1, gray);
s2 = subplot(y,x,2); imagesc(rad);
colormap(s2, hot);
colorbar;

line_img = zeros(128, 128);
line_img(64, 32:64) = 0.5;
line_img(63, 32:64) = 0.4;
line_img(63, 32:64) = 0.2;
line_rad = radon(line_img);
s3 = subplot(y,x,3); imshow(line_img);
colormap(s3, gray);
s4 = subplot(y,x,4); imagesc(line_rad);
colormap(s4, hot);

colorbar;

Any help is appreciated. I expect to have source images in grayscale and radon transform images in "hot". For some reason I'm getting first subplot somewhat like grayscale (it's actually not, since I initialize point with value 0.2 and octave gives me pure white color for it, while I'm expecting reasonably dark gray), and thee remaining images seem to have "hot" colormap being set.

Upvotes: 0

Views: 1505

Answers (1)

Suever
Suever

Reputation: 65430

If you read that line from the documentation a little close you will see that is that that if you pass an axes handle that the parent figure's colormap is changed. This is not the same as each axes having a different colormap since they're all in the same figure.

Function File: cmap = colormap (hax, …) If the first argument hax is an axes handle, then the colormap for the parent figure of hax is queried or set.

Currently, Octave does not support this functionality which was only just recently introduced in MATLAB.

The way around this is to convert your images to RGB images prior to displaying with imshow and then the figure's colormap is irrelevant. You can do this by first converting it to an indexed image (using gray2ind) and then to RGB with ind2rgb.

% To display the grayscale image
rgb = ind2rgb(gray2ind(img), gray);
imshow(rgb);

As a side note, the reason that your first grayscale image is showing up as all white is that if the input to imshow is of type double, then all values are expected to be between 0 and 1. If you want to change this behavior you can use the second input of imshow to specify that you'd like to scale the color limits to match your data

imshow(img, [])

Upvotes: 4

Related Questions