在Matlab中获取颜色条的完整大小 [英] Obtain full size of colorbar in Matlab

查看:141
本文介绍了在Matlab中获取颜色条的完整大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am writing a plot automation routine for Matlab. However, I am having issues to assess the (horizontal) size of colorbars. I can use the following to get the size of the colorbar:

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

这将为我提供颜色条的水平尺寸,但不包括标签和刻度标签.

This will give me the horizontal size of the colorbar, but EXCLUDES labels and tick labels.

您知道如何获取条形图和标签的完整大小吗?

Do you have an idea how to obtain the full size of both the bar and the labels?

预先感谢

推荐答案

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

In versions of MATLAB prior to R2014b, a colorbar was simply an axes object in disguise so you could easily use the OuterPosition property of the colorbar to get the position of the colorbar (including labels and tick labels). However, in R2014b, the colorbar is it's own graphics object and the underlying axes is no longer accessible.

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

One possible workaround is to create an invisible axes object on top of the colorbar (that has the same tick marks and labels) and get the OuterPosition of that.

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

这篇关于在Matlab中获取颜色条的完整大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆