【问题标题】:Making a colorbar's frame white while keeping the labels black使颜色条的框架变白,同时保持标签为黑色
【发布时间】:2021-02-28 11:01:13
【问题描述】:

如何使颜色条框周围的矩形线变为白色,但保持刻度标签为黑色?

下面的可重现代码:

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

【问题讨论】:

  • 感谢@LuisMendo,我修改了问题以使其更清晰
  • 请提及您的 MATLAB 版本。另外,你用的是图形还是 uifigures?

标签: matlab border customization matlab-figure colorbar


【解决方案1】:

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

    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)

【讨论】:

  • 非常感谢@wwweagle,这是一个很好的解决方法,可以产生预期的效果。
  • 如果在添加注释后调整图形大小,这是否保留在正确的位置?如果没有,我会补充一点,这需要在调整大小后执行。
  • @Dev-iL 您的解决方案显然更好(并且被赞成),但是您从哪里学到这些未记录的功能:)?
  • @wwweagle 真的,我认为您的解决方案是完全有效的,只需稍加说明即可受益。感谢投票!我最初是通过阅读 UndocumentedMATLAB 博客来学习的,后来通过反复试验(struct 函数是你挖掘时最好的朋友)。在许多类似的例子之后,我意识到总是有一些“原始”对象的引用,例如一行或一个补丁,其属性或方法需要访问才能成功。如果您想进一步讨论此类问题,请随时通过the MATLAB chatroom 停下来。
【解决方案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

导致:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-12
    • 2011-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-08
    • 2012-02-13
    相关资源
    最近更新 更多