MATLAB Answers

How to remove the margins of a plot

314 views (last 30 days)
Hao Shi
Hao Shi on 25 Jun 2020
Answered: Deepak Meena on 24 Sep 2020
Hi there,
I have created a plot with the blow code, but there are still some additional margins on the left and right, could you tell me how to remove them?
I have tried it with setting the gca position, but it only works for the up and down side.
Here is the code I used:
% Data
xData = [0;42.5;0;-42.5];
yData = [42.5;0;-42.5;0];
zData = [0.5695;0.1766;-0.2984;-0.0129];
minFilterPressure = min(zData);
maxFilterPressure = max(zData);
% Set up fittype and options.
ft = 'biharmonicinterp';
% Fit model to data.
[fitresult, gof, output] = fit( [xData, yData], zData, ft, 'Normalize', 'on' );
% Plot the fit contour
axisLimit = axis;
patch([42.5*cos(0:0.01:0.5*pi) 42.5], [42.5*sin(0:0.01:0.5*pi) 42.5], [1 1 1])
patch([42.5*cos(0.5*pi:0.01:pi) -42.5], [42.5*sin(0.5*pi:0.01:pi) 42.5], [1 1 1])
patch([42.5*cos(pi:0.01:1.5*pi) -42.5], [42.5*sin(pi:0.01:1.5*pi) -42.5], [1 1 1])
patch([42.5*cos(1.5*pi:0.01:2*pi) 42.5], [42.5*sin(1.5*pi:0.01:2*pi) -42.5], [1 1 1])
axis equal
text((axisLimit(2) + 5),0,'Side2','VerticalAlignment','middle','HorizontalAlignment','center','Fontsize',12,'FontWeight','bold','Rotation',-90);
set(gca, 'Position', [0,0.1,1,0.85])
Here is the generated plot:


Walter Roberson
Walter Roberson on 25 Jun 2020
Look at the axes properties InnerPosition and OuterPosition
Hao Shi
Hao Shi on 25 Jun 2020
Dear Walter and KSSV,
Thank you very much for your kind suggestions.
For Walter's suggestion, I have tried to set both of the gca position and outerPosition, but there seems no big changes, the left and right margins are still there, the parameters I set are listed below. Could you help me to find where the problem is?
set(gca, 'Position', [-0.1,0.1,1.2,0.85])
set(gca, 'OuterPosition', [0,0,1,1])
For KSSV's suggestion, the command is valid since the version 2020a, which does not work on my version.

Sign in to comment.

Answers (1)

Deepak Meena
Deepak Meena on 24 Sep 2020
Hi Hao Shi,
Add these line of code to your answer :
set(gca, 'Position', [0.02,0.1,1,0.82])
set(gcf,'OuterPosition',[50 50 680 700]);
ax = gca;
outerpos = ax.OuterPosition;
ti = ax.TightInset;
left = outerpos(1) + ti(1);
bottom = outerpos(2) + ti(2);
ax_width = outerpos(3) - ti(1) - ti(3);
ax_height = outerpos(4) - ti(2) - ti(4);
ax.Position = [left bottom ax_width ax_height];
For more details about the properties of axes , refer to this document


Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!