如何在 MATLAB 图 window 的边缘周围创建边框?

How can I create a border around the edge of a MATLAB figure window?

我想在将绘图保存为 .png 之前在图形周围加上边框,以突出显示一些最重要的绘图。有没有什么办法可以在坐标轴绘图区域之外绘制一个矩形?

我希望边框围绕整个图延伸,甚至包括图标题和轴标签。

您可以通过将轴放在 uipanel and adjusting the panel position, edge design, figure color, and panel colors 内来创建各种边框类型。例如,这将创建一个带有 beveled-in 边缘的宽青色边框,该边缘从面板边缘延伸到图形边缘:

hFigure = figure('Color', 'c');  % Make a figure with a cyan background
hPanel = uipanel(hFigure, 'Units', 'normalized', ...
                 'Position', [0.1 0.1 0.8 0.8], ...
                 'BorderType', 'BeveledIn');  % Make a panel with beveled-in borders
hAxes = axes(hPanel, 'Color', 'none');  % Set the axes background color to none
title('Title Here');


这会创建一个 5 像素宽的红线边框,紧贴图形的边缘:

hFigure = figure();  % Make a figure
hPanel = uipanel(hFigure, 'Units', 'normalized', ...
                 'Position', [0 0 1 1], ...
                 'BorderType', 'line', ...
                 'BorderWidth', 5, ...
                 'BackgroundColor', 'w', ...
                 'HighlightColor', 'r');  % Make a white panel with red line borders
hAxes = axes(hPanel, 'Color', 'none');  % Set the axes background color to none
title('Title Here');

找到解决方案。将绘图保存到图像后,您可以将其重新加载到图形中,然后在图像顶部绘制边框。

img = imread('test_image.png');
fh = figure;
imshow(img,'border','tight')
hold on;
figurepos = get(gcf,'Position');
rectangle('Position',[4 4 figurepos(3)-7 figurepos(4)-7],'LineWidth',5,'EdgeColor','red')

两个选项:

1- 将Clipping axes property设置为'off',并在轴边界外绘制一个矩形。您必须使用轴的单位找出正确的位置。要在不同的图表中保持一致,这可能有点挑战。

2- 创建一个辅助轴,使其不可见,调整其大小以占据整个图形,并在其中绘制一个矩形:

f = figure
% One axes is invisible and contains a blue rectangle:
h = axes('parent',f,'position',[0,0,1,1],'visible','off')
set(h,'xlim',[0,1],'ylim',[0,1])
rectangle(h,'position',[0.01,0.01,0.98,0.98],'edgecolor',[0,0,0.5],'linewidth',3)
% Another axes is visible and you use as normal:
h = axes('parent',f)
plot(h,0:0.1:10,sin(0:0.1:10),'r-')

(我在这里明确使用 fh 作为 "parent" 对象,因为这通常会导致更健壮的代码,但您当然可以将它们排除在外,并依赖于大多数时候隐式使用 gcfgca 来做正确的事情。)