MATLAB Answers

# How to eliminate gap between tile charts

19 views (last 30 days)
Mengyuan Li on 13 May 2021
Edited: Adam Danz on 17 May 2021
Hey guys,
I am trying to create a graph with 3 sections, each with a different x axis but the y axis is the same throughout. I used tile chart layout learnt from https://au.mathworks.com/help/matlab/creating_plots/graph-with-multiple-x-axes-and-y-axes.html and set TileSpacing = 'none', but there is still a gap between the sections as shown in the picture. How do you eliminate the gaps between the sections?
x = 0:0.1:60;
y1 = 4.*cos(x)./(x+2);
y2 = 5*sin(x)+0.5
figure
t = tiledlayout(1,3,'TileSpacing','none'); %no tile spacing?
bgAx = axes(t,'XTick',[],'Ytick',[],'Box','off');
bgAx.Layout.TileSpan = [1 3];
%plot part 1
ax1 = axes(t);
plot(ax1,x,y2);
xline(ax1,15,':');
ax1.Box = 'off';
xlim(ax1,[0 15])
xlabel(ax1,'First Interval')
%plot part 2
ax2 = axes(t);
ax2.Layout.Tile = 2
plot(ax2,x,y1)
xline(ax2,15,':');
xline(ax2,45,':');
ax2.YAxis.Visible = 'off';
ax2.Box = 'off';
xlim(ax2,[15 45])
xlabel(ax2, 'Second Interval')
%plot part 3
ax3 = axes(t);
ax3.Layout.Tile = 3
plot(ax3,x,y2)
xline(ax3,50,':');
ax3.YAxis.Visible = 'off';
ax3.Box = 'off';
xlim(ax3,[50 60])
xlabel(ax3, 'Third Interval')
%link axes
linkaxes([ax1 ax2 ax3],'y')
##### 3 CommentsShowHide 2 older comments
Scott MacKenzie on 15 May 2021
I'm running R2021a. Clearly, Adam's answer is spot on. Good luck.

Sign in to comment.

### Accepted Answer

Adam Danz on 14 May 2021
Edited: Adam Danz on 14 May 2021
Update to Matlab R2021a to take advantage of the new tile spacing when TileSpacing is set to none.
The only other way to set juxtaposed axes is by setting axes position manually this template:
nAxes = 3;
axXPos = linspace(0.05, .95, nAxes+1); % For axes that span 0.05 to 0.95 horizontally
axXPos(end) = [];
axWidth = axXPos(2) - axXPos(1);
ax = gobjects(size(axXPos));
for i =1:numel(axXPos)
ax(i) = axes('Units','Normalized','Position',[axXPos(i),.2,axWidth,.4]);
box(ax(i), 'on')
end
set(ax(2:end),'YTickLabel', {})
linkaxes(ax,'y') % If you want to use the same y-ticks for all axes
To turn off y-axis lines,
ax(i).YAxis.Visible = 'off';
##### 2 CommentsShowHide 1 older comment
Adam Danz on 15 May 2021
TileSpacing works differently in versions prior to R2021a. The link I provided explains that.
TileSpacing=none in releases prior to R2021a is the same as TileSpacing=Tight in Matlab R2021a+ and "none" was redfined as having no space between axes.

Sign in to comment.

R2019b

### Community Treasure Hunt

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

Start Hunting!