【问题标题】:How can I create a border around the edge of a MATLAB figure window?如何在 MATLAB 图形窗口的边缘创建边框?
【发布时间】:2018-04-05 17:53:54
【问题描述】:

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

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

【问题讨论】:

  • 我希望边框围绕整个绘图延伸,包括标题和轴标签,所以我认为 'box' 命令不适用于这种情况

标签: matlab plot border matlab-figure


【解决方案1】:

您可以通过将坐标轴放在uipanel 内并调整panel positionedge designfigure colorpanel colors 来创建各种边框类型。例如,这会创建一个宽的青色边框,其边缘从面板边缘延伸到图形边缘:

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');

【讨论】:

    【解决方案2】:

    两种选择:

    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 作为“父”对象,因为这通常会导致更健壮的代码,但您当然可以将它们排除在外,并依赖于隐式使用的 gcf 和 @ 987654327@大多数时候做正确的事。)

    【讨论】:

      【解决方案3】:

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

      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')
      

      【讨论】:

        猜你喜欢
        • 2016-10-02
        • 2011-05-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-04-02
        • 2016-01-30
        相关资源
        最近更新 更多