Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 718
SirWiggles Avatar asked Jan 21 '26 03:01

SirWiggles


2 Answers

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

enter image description 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');

enter image description here

like image 186
gnovice Avatar answered Jan 23 '26 21:01

gnovice


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

like image 45
Cris Luengo Avatar answered Jan 23 '26 21:01

Cris Luengo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!