Katie
Katie

Reputation: 65

Matlab colorbar pcolor

I've got a map showing tidal mixing fronts in pcolor, and I want to customise the colourbar.

Values of zero should be grey. Values between 0.1 and 2.5 should be white. Anything between 2.5 and 6 should be colourful (like the jet colorbar or something).

So far, I've tried the code below, which works, but ideally I'd like the coloured bit to be more of a gradient than block colours.

pcolor(log10(hu'));
shading interp;
caxis([0 6]);
map = [0 0 0
    0.9 0.9 0.9
    0.9 0.9 0.9
    0.9 0.9 0.9
    0 0 0.3
    0 0 0.6
    0 0 0.9];
colormap(map);

Any advice appreciated! Thanks

Upvotes: 0

Views: 665

Answers (2)

Hoki
Hoki

Reputation: 11812

You can just build your custom colormap by respecting the proportions of the different intervals you want to separate.

For your example, with data bound by [0 6] domain, you could choose a simple 60 colors (=60 lines) colormap.

  • The first line (mapped to value 0) is set to grey
  • The lines 1 to 25 (mapping the values from 0.1 to 2.5) should be white
  • The lines 26 to 60 (mapping the values from 2.6 to 6.0) should be a gradient colormap

Of course if you want increased resolution you can multiply these numbers of lines by a coefficient, as long as the ratio of number of lines is respected.

To see it in code, I first needed to create some sample data (you did not provide a minimal working example!!):

%% Sample data
camax = 6 ;
Z = peaks(50) ;
% normalise Z so the data are bound within [0 6]
zmax = max(max(Z)) ; zmin = min(min(Z)) ; zspan = zmax-zmin ;
Zn = ((Z-zmin)./zspan ) * camax ;
% plot
hp=pcolor(Zn);
shading interp;
hb = colorbar ;

This produces the figure below on the left.

Now to create and apply your custom colormap:

%% Build and apply colormap
granularity_factor = 1 ;    % increase that for higher resolution

levels  = [0 0.1 2.5 6] ;   % Your different levels
dl      = diff(levels) ;    % The "span" of each interval between level = [0.1, 2.4, 3.5]

color0 = [.7 .7 .7] ;   % Grey
color1 = [1 1 1] ;      % White

% Number of lines in the colormap for each interval
nlines = round( dl * 10 * granularity_factor ) ; % = [1, 24, 35] lines for each interval

% Now build the colormap
cmap = [
    repmat( color0 , nlines(1) ,1 ) ; ...   % 1 line of this color
    repmat( color1 , nlines(2) ,1 ) ; ...   % 24 lines of this color
    parula( nlines(3) ) ...                 % 35 lines of "parula" colormap
    ] ;
    
colormap(cmap)                  % Apply it
hb.Ticks = [0.1 2.5:0.5:6] ;    % Adjust ticks on colorbar (optional)

This will now produce the figure below on the right.

enter image description here

And as I said, increase the granularity_factor if you need a more refined colormap.


Edit: I should have known that this would already be on here. You can see an almost duplicate/similar question and it's answer using the same technique I just described: How to control colorbar color ranges in Matlab plots?

Upvotes: 1

Ander Biguri
Ander Biguri

Reputation: 35525

If you want the same colormap, just smoother:

hsv=rgb2hsv(map);
map=interp1(linspace(0,1,size(map,1)),hsv,linspace(0,1,desired_colormap_size));
map=hsv2rgb(map);

Note that this may cause some weird color jump, as H is a circular value (360==0) and interp1 does not take that into account. It will depend on your color.

Upvotes: 0

Related Questions