How to eliminate gap between tile charts

113 次查看(过去 30 天)
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 个评论
Scott MacKenzie
Scott MacKenzie 2021-5-15
I'm running R2021a. Clearly, Adam's answer is spot on. Good luck.

请先登录,再进行评论。

采纳的回答

Adam Danz
Adam Danz 2021-5-14
编辑:Adam Danz 2021-5-14
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 个评论
Mengyuan Li
Mengyuan Li 2021-5-15
Thanks Adam! So I assume TileSpacing = 'none' does not work properly in older versions?
Adam Danz
Adam Danz 2021-5-15
编辑:Adam Danz 2021-5-17
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Line Plots 的更多信息

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by