Figure & Subplot: Getting rid of needless white space

8 views (last 30 days)
Hello, I am currently trying to find out why there is excessive white space left and right of the plots when the spacing was just fine for a single plot in the figure. Please excuse the lengthy example. I am unsure what exactly causes the strange spacing. Hopefully you can give me hint, what to look into. (as at the moment I am cropping it manually..)
btw - Loose and TightInset did not work for me yet, but it seems quite possible that I implemented them in a wrong way as the figure was either empty or cut off vertical space (instead of horizontal).
Best Regards
fig = figure();
set(fig,'color', none,'position',[100 100 1200 400]);
set(gca,'LooseInset',get(gca,'TightInset')) % my try to crop the figure
subplot(1,2,1)
p = plot(x, y1, x, y2, x, y3, x, y4);
ylabel('label1 (unit)', 'interpreter', 'latex', 'fontsize', 20)
xlabel('label2 (unit)', 'interpreter', 'latex', 'fontsize', 20)
set(gca, 'fontsize', 20);
set(gca, 'XTick', [0:2.5:10], 'xlim', [0 10]);
set(gca, 'YTick', [0:25:100], 'ylim', [0 100]);
grid on
subplot(1,2,2)
q = plot(x2, y5, x2, y6, x2, y7, x2, y8);
ylabel('label1 (unit)', 'interpreter', 'latex', 'fontsize', 20)
xlabel('label2 (unit)', 'interpreter', 'latex', 'fontsize', 20)
set(gca, 'fontsize', 20);
set(gca, 'XTick', [0:2.5:10], 'xlim', [0 10]);
set(gca, 'YTick', [0:100:400], 'ylim', [0 400]);
grid on
save_fig(fig, 'test');

Answers (1)

Thorsten
Thorsten on 1 Aug 2016
Try FEX submission tight_subplot
  2 Comments
tapdanceotter
tapdanceotter on 1 Aug 2016
Thorsten, thank you for the suggestion. I am looking into TightFig by Crozier but (as indicated by some comments there) there are difficulties because it also cuts away the axis labels. If you worked with this before, do you know if the following code snippet is the part where this would need to be reconfigured and if so, how?
% we will move all the axes down and to the left by the amount
% necessary to just show the bottom and leftmost axes and labels etc.
moveleft = min(pos(:,1) - ti(:,1));
movedown = min(pos(:,2) - ti(:,2));
% we will also alter the height and width of the figure to just
% encompass the topmost and rightmost axes and lables
figwidth = max(pos(:,1) + pos(:,3) + ti(:,3) - moveleft);
figheight = max(pos(:,2) + pos(:,4) + ti(:,4) - movedown);

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!