Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to apply different colormaps in different subplots?

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?

like image 902
natario Avatar asked Mar 29 '15 15:03

natario


2 Answers

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

like image 143
Robert Seifert Avatar answered Oct 31 '22 23:10

Robert Seifert


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.

like image 43
Phil Goddard Avatar answered Oct 31 '22 23:10

Phil Goddard