How to apply different colormaps in different subplots?

natario picture natario · Mar 29, 2015 · Viewed 8.2k times · Source

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?

Answer

thewaywewalk picture thewaywewalk · Mar 29, 2015

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