简体   繁体   中英

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

I'd like to put a border around a figure before saving my plots to .png for highlighting some plots that are most important. Is there any way to draw a rectangle outside of the axes plot region?

I'd like the border to extend around the entire plot even including the plot title and axes labels.

You can create a variety of border types by placing the axes inside a uipanel and adjusting the panel position , edge design , figure color , and panel colors . For example, this creates a wide cyan border with a beveled-in edge that extends from the panel edge to the figure edge:

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

在此输入图像描述


And this creates a 5-pixel wide red line border tight to the edge of the figure:

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

在此输入图像描述

Two options:

1- Set the Clipping axes property to 'off' , and draw a rectangle outside of the axes bounds. You'll have to figure out what the right position is, using the units of the axes. This could be a little challenging to do consistently across different graphs.

2- Create a secondary axes, make it invisible, size it to occupy the full figure, and draw a rectangle in it:

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

(I've explicitly used f and h as "parent" objects here because that typically leads to more robust code, but you can certainly leave those out, and depend on the implicitly used gcf and gca to do the right thing most of the time.)

Found the solution. After saving a plot to an image, you can reload it into a figure and then draw a border on top of the image.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM