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

4 次查看(过去 30 天)
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

回答(1 个)

Benjamin Großmann
Benjamin Großmann 2018-4-23
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.

类别

Help CenterFile Exchange 中查找有关 Grid Lines, Tick Values, and Labels 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by