Rolando Gonzales
Rolando Gonzales

Reputation: 512

Making a colorbar's frame white while keeping the labels black

How can I make the rectangular line around the box of the colorbar white, but keep the tick labels black?

Reproducible code below:

colormap(flipud(autumn(6))); % Create Colormap
         cb = colorbar; % Create Colorbar         
         cb.Ticks = [.1667 .3334 .5001 .6668 .8335]; % Create ticks
         cb.TickLabels = ({'0%','5%','10%','15%','20%'});    
         cb.TickLength = 0;
         cb.Color = 'w'; % Everything becomes white, I only want the rectangular color of the box to be white

Upvotes: 2

Views: 284

Answers (2)

Dev-iL
Dev-iL

Reputation: 24159

From the ColorBar properties documentation page we learn that the Color property changes too many things:

Color — Color of tick marks, text, and box outline

So we have to try something else. Fortunately, if you are comfortable with using an undocumented feature, the desired effect can be achieved by modifying the colorbar's Ruler property:

function [] = q66408322()
hF = figure();
hAx = axes(hF);
colormap(hAx, flipud(autumn(6)));
hCb = colorbar(hAx,...
  'Ticks', [.1667 .3334 .5001 .6668 .8335],...   
  'TickLabels', {'0%','5%','10%','15%','20%'}, ...
  'TickLength', 0, ...
  'Color', 'w');
hCb.Ruler.Color = 'k'; % <<<<<< Solution

Resulting in:

enter image description here

Upvotes: 3

X Zhang
X Zhang

Reputation: 1325

You could get the wanted visual effect by overlaying a white annotation box on the colorbar, e.g.

    colormap(flipud(autumn(6))); % Create Colormap
             cb = colorbar; % Create Colorbar         
             cb.Ticks = [.1667 .3334 .5001 .6668 .8335]; % Create ticks
             cb.TickLabels = ({'0%','5%','10%','15%','20%'});    
             cb.TickLength = 0;
             % cb.Color = 'w'; % Everything becomes white, I only want the rectangular color of the box to be white

    annotation('rectangle',cb.Position,'Color','w','FaceColor','none','LineWidth',2)

Upvotes: 3

Related Questions