How to apply different colormaps in different subplots?
Asked Answered
I

3

5

I'm doing more or less the following:

figure
for ii=1:4
    subplot(2,2,ii)
    imshow(image(ii))
    hcb = colorbar;

    switch ii
        case 1
            colormap(myMap)
            set(hcb,'YTickLabel', .. )
            set(hcb,'YTick', .. )
        case 2
            colormap(myMap)
            set(hcb,'YTickLabel', .. )
            set(hcb,'YTick', .. )
        case 3
            colormap(myMap)
            set(hcb,'YTickLabel', .. )
            set(hcb,'YTick', .. )
        case 4
            colormap(aDifferentMap)
            set(hcb,'YTickLabel', .. )
            set(hcb,'YTick', .. )
    end
end

What I'm facing is that calling colormap(aDifferentMap) for the fourth plot (ii=4), screws things up for the previous three plots: in my final figure all colorbars have aDifferentMap colormap, with also some problems to the YTick attribute.

If I comment out colormap(aDifferentMap) in case 4, it all works well (except for the fourth subplot, which will have a wrong colormap and no Ytickes whatsoever).

How can I deal with this? How can I set properties of subplot(2,2,4) without influencing subplots 1:3?

Ir answered 29/3, 2015 at 15:46 Comment(0)
B
6

For Matlab 2014a and before applies the answer of Phil Goddard and you need to use e.g. freezeColors from FileExchange.


In Matlab 2014b the problem got solved with the update of the graphics engine to version HG-2. Now the colormap affects all axes in the figure, unless you set an axes colormap separately. (from doc)

figure
ax1 = subplot(2,1,1);
surf(peaks)
colormap(ax1,spring)

ax2 = subplot(2,1,2);
surf(peaks)
colormap(ax2,winter)

enter image description here

Bivens answered 29/3, 2015 at 16:3 Comment(2)
After a quick check in meta, I found this is the answer I should accept as it is more complete and probably more useful for future readers. However, being on R2011a, I found my solution through Phil Goddard's link. Namely I used both freezeColors and cbfreeze tools, as suggested by freezeColors author.Ir
It would be absolutely fine to accept Phil's answer, as he solved your problem first. I just felt his answer his answer wasn't complete, especially because its recent changes in validity.Bivens
A
1

Colormap is a property of the figure, not the axes, so changing it for a subplot changes it for all subplots.

Have a look at Using multiple colormaps in a single figure for an example of a solution.

Alf answered 29/3, 2015 at 15:52 Comment(0)
P
0

You can use ind2rgb if you just want to show images with different colormaps in a figure:

load flujet;
subplot(221); image(ind2rgb(X, gray(63)));
subplot(222); image(ind2rgb(X, jet(63)));
subplot(223); image(ind2rgb(X, hot(63)));
subplot(224); image(ind2rgb(X, copper(63)));

However, different colorbars still can't be shown in earlier versions of MATLAB.

Puttee answered 14/5, 2016 at 17:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.