将颜色条的框架设为白色,同时将标签保持为黑色

时间:2021-02-28 11:01:13

标签: matlab border customization matlab-figure colorbar

如何将颜色栏框周围的矩形线设为白色,但将刻度标签保持为黑色?

以下可重现代码:

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

2 个答案:

答案 0 :(得分:3)

您可以通过在颜色栏上覆盖一个白色的注释框来获得想要的视觉效果,例如

    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)

答案 1 :(得分:2)

ColorBar properties documentation page 我们了解到 Color 属性改变了太多东西:

<块引用>

Color — 刻度线、文本和框轮廓的颜色

所以我们必须尝试其他的东西。幸运的是,如果您习惯于使用未记录的功能,则可以通过修改颜色栏的 Ruler 属性来实现所需的效果:

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

结果:

enter image description here