How to remove whitespace around subplots keeping margines for writing x/y labels and ticks?

1 view (last 30 days)
shifu
shifu on 21 Mar 2018
Answered: Benjamin Großmann on 23 Apr 2018
I am generating a map in in MATLAB 2016b with 12 subplots. I am required to remove the whitespace between subplots for which I am using subplot_tight function. Apparently this function doesn't allow any space to put xlabel or y label, which is required.
In the attached image one can see how the X-axis and Y-axis ticklabels are getting omitted.
How to keep include white space to keep x-label and y-labels?
Below is my script,
margins=[0.0004,0.04];
for i=1:12
if mod(i,2)==1
subplot_tight(6,2,i, margins)
imagesc(A.data.Zone1)
colormap(gca, brewermap([],'Blues'))
elseif mod(i,2)==0
subplot_tight(6,2,i, margins)
imagesc(B.data.Zone1)
colormap(gca, brewermap([],'Greens'))
end
if i==11 || i==12
set(gca, 'XTick', 1:1:12, 'XTickLabel',{'A','B','C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L'});
else
set(gca, 'XTickLabel',[]);
end
if ismember(i, [1:2:11])
set(gca, 'YTick', 1:1:10, 'YTickLabel',{'M1','M2','M3', 'M4', 'M5', 'M6', 'M7', 'M8', 'M9', 'M10'});
else
set(gca, 'YTickLabel',[]);
end
set(gca, 'TickLength', [0.0001, 0.0001])
end

Answers (1)

Benjamin Großmann
Benjamin Großmann on 23 Apr 2018
For such tasks, i prefer using the OuterPosition property of axes. I.e.:
clearvars
close all
clc
f = figure;
for ii = 0:5
for jj = 0:1
ax = axes('Parent',f,...
'Units','Normalized',...
'OuterPosition',[jj/2 ii/6 1/2 1/6],...
'XTickLabels','',...
'YTickLabels','');
xlim([0,400])
ylim([0,200])
ax.XLabel.String = 'xLabel';
ax.YLabel.String = 'yLabel';
hold(ax,'all')
imagesc(rand(200,400))
end
end
Feel free to substitute the ugly for-loops by cellfun and/or arrayfun.

Tags

Community Treasure Hunt

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

Start Hunting!