【问题标题】:Obtain full size of colorbar in Matlab在 Matlab 中获取颜色条的全尺寸
【发布时间】:2017-03-09 10:19:27
【问题描述】:

我正在为 Matlab 编写一个绘图自动化例程。 但是,我在评估颜色条的(水平)大小时遇到​​问题。 我可以使用以下内容来获取颜色条的大小:

cb = findall(groot,'Type','colorbar'); % get colorbar
xwidth = cb.Position(3);

这会给我颜色条的水平尺寸,但不包括标签和刻度标签。

您知道如何获得条形图和标签的完整尺寸吗?

提前致谢

【问题讨论】:

  • 什么版本的 MATLAB?
  • @Suever: 2017a 预发布
  • 一般来说,由于机密性质,最好不要在公共论坛上询问有关预发布软件的问题。话虽如此,这个问题适用于 R2014b 之后的所有 MATLAB 版本。

标签: matlab plot position matlab-figure colorbar


【解决方案1】:

在 R2014b 之前的 MATLAB 版本中,颜色条只是伪装的 axes 对象,因此您可以轻松地使用颜色条的 OuterPosition 属性来获取颜色条的位置(包括标签和刻度标签)。但是,在 R2014b 中,颜色条是它自己的图形对象,并且不再可以访问基础轴。

一种可能的解决方法是在颜色栏顶部创建一个不可见的axes 对象(具有相同的刻度线和标签)并获取那个OuterPosition

function pos = getColorbarPosition(cb)

    tmp = axes('Position', cb.Position, 'YAxisLocation', 'right', ...
            'YLim', cb.Limits, 'FontSize', cb.FontSize, 'Units', cb.Units, ...
            'FontWeight', cb.FontWeight, 'Visible', 'off', ...
            'FontName', cb.FontName, 'YTick', cb.Ticks, ...
            'YTickLabels', cb.TickLabels, 'XTick', []);

    if ~isempty(cb.Label)
        ylabel(tmp, cb.Label.String, 'FontSize', cb.Label.FontSize, ...
        'FontWeight', cb.Label.FontWeight, 'FontWeight', cb.Label.FontWeight)
    end

    pos = get(tmp, 'OuterPosition');

    delete(tmp);
end

【讨论】:

  • 我已经预料到一个有点冗长的解决方法,但这对我有用,非常感谢!
  • 我发现你还需要在轴初始化中添加'TickDir',cb.TickDirection
【解决方案2】:

在 matlab2017 中,颜色条对象有两个重要的大小属性,“Position”和“Label.Extent”

cax = colorbar;
cax.Units = 'centimeters'; % I think this sets the units for the child
cax.Label.String = 'A title'; 
% The position of the bar itself as [ left bottom width height ]
cpos = cax1.Position; 
% The position of the label as [ left bottom width height ]
lpos = cax.Label.Extent;
% The width of the colorbar and label is:
totalwidth = cpos(3) + lpos(3)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-19
    • 1970-01-01
    • 1970-01-01
    • 2020-11-19
    • 2018-01-13
    • 2018-10-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多