如何将 marker/symbol 添加到颜色栏

How to add marker/symbol to colorbar

我想在我的 MATLAB colorbar 中添加一个 marker/special 刻度线。例如,假设我有一个从 -23 的颜色条刻度,我的临界值为 -1.8,我如何通过 symbol/marker 添加值的标记?

您可以简单地在绘图区域外添加 text 并将其用于您的标记:

A = rand(15);
f=figure;
imagesc(A);
text(7,7,'X','Color','red') % Put a red X in the middle
text(17.5,size(A,1)*(1-A(7,7)),'<marker'); % Set a marker on the colour bar for the middle point
colorbar

这导致:

我还没有找到在 颜色条上绘制标记 的方法,因为颜色条不是我要绘制的子图之一出于某种原因可以在 uistack 中使用。

一种方法是在此处获取 colorbar position, compute the location where you want your marker, and place an annotation object(如箭头):

% Plot sample data, displaying color bar and getting its limits:

data = peaks();
imagesc(data);
hBar = colorbar();
cLimits = caxis();
hold on;

% Select and plot a point of interest:

point = [31 15];
value = data(point(1), point(2));
plot(point(2), point(1), 'r+');

% Compute location of color bar pointer and make annotation:

barPos = get(hBar, 'Position');
xArrow = barPos(1)+barPos(3)/2+[0.05 0];
yArrow = barPos(2)+barPos(4)*(value-cLimits(1))/diff(cLimits)+[0 0];
hArrow = annotation('textarrow', xArrow, yArrow, ...
                    'String', num2str(value, '%.2f'), 'Color', 'r');

如果调整图形大小,注释对象的位置可能会相对于颜色条移动。避免这种情况的一种方法是使用以下代码调整轴和颜色条调整大小行为:

axesPos = get(gca, 'Position');
set(hBar, 'Location', 'manual');
set(gca, 'Position', axesPos);

这应该允许注释对象固定在颜色条上的正确位置。

这是另一个选项 - 只是 add/change 特定值的刻度标签:

MarkTxt = '<-Mark';
imagesc(rand(10)-2) % something to plot
colormap('jet')
CB = colorbar;

% in case you DON'T want the value to appear:
value = -1.8;
t = find(CB.Ticks==value);
if ~isempty(t)
    CB.TickLabels{t} = MarkTxt;
else
    [CB.Ticks,ord] = sort([CB.Ticks value],'ascend');
    t = CB.Ticks==value;
    CB.TickLabels{t} = MarkTxt;
end

%% OR - in case you want the value to appear:
value = -1.24;
t = find(CB.Ticks==value);
if ~isempty(t)
    CB.TickLabels{t} = [CB.TickLabels{t} MarkTxt];
else
    [CB.Ticks,ord] = sort([CB.Ticks value],'ascend');
    t = CB.Ticks==value;
    CB.TickLabels{t} = [CB.TickLabels{t} MarkTxt];
end

没有值:

价值: