在 Matlab 图形中绘制图例

Draw over legend in a Matlab figure

我想在 matlab 图中的图例上放置一个箭头,但是当我添加箭头时,图例默认为 "on top"(见图,图例覆盖了黑线) .

有没有办法将箭头等子图形推到 "top" 上,使其出现在图形的所有其他组件(包括图例)之上?我试过使用 uistack 但这似乎不适用于图例。 uistack 正如文档所说应该“重新排列 UI 组件的视觉堆叠”。

编辑:

非常简单的例子:我画的线应该出现在图例的顶部。

figure;
b = bar(1:3,rand(3));
hold on;
p = plot([0,3],[0,.5],'Color','k','linewidth',1.5); % my arrow
l = legend(b,'value','Location','SouthWest','AutoUpdate','off');
uistack(l,'bottom');

您使用的是哪个 MATLAB 版本? uistack 似乎不再适用于图例,因为 MATLAB 2015b(参见类似 problem)。

如果如您所说,线条可以出现在任何地方,最好的解决方法可能是选择 best 图例位置

l = legend(b,'value','Location','Best','AutoUpdate','off');

您可以将图例背景设为透明 - 这样您就可以看到穿过图例的箭头

figure;
b = bar(1:3,rand(3));
hold on;
p = plot([0,3],[0,.5],'Color','k','linewidth',1.5); % my arrow
l = legend(b,'value','Location','SouthWest','AutoUpdate','off');
l.BoxFace.ColorData = uint8([255 255 255 127]');
l.BoxFace.ColorType = 'truecoloralpha';

ColorData 属性 是 [R G B 透明度]

供参考:这是使用 R2015b 完成的。

您可以copyobj当前图形轴gca并将其Color属性设置为none。此方法将在图例的补丁和相关文本上绘制线条。

说明:Copyobj 将复制并显示所有与 barline 有关的 axes 但不包括图例(图例上有 axes他们自己的)。复制的 axes 的显示将与原始的完美叠加。并且 'Color','none' 使复制的 axes 的白色背景透明,从而使 legend 再次可见,但在 行可见。

这是代码

f = figure;
b = bar(1:3,rand(3));
hold on;
p = plot([0,3],[0,.5],'Color','k','linewidth',1.5); % my arrow
l = legend(b, 'Location','SouthWest');

% add some magic
hax = copyobj(gca, f); % copy the current axes to the figure
set(hax, 'Color', 'none') % set the new axes's background transparent