How to eliminate gap between tile charts

114 Ansichten (letzte 30 Tage)
Mengyuan Li
Mengyuan Li am 13 Mai 2021
Bearbeitet: Adam Danz am 17 Mai 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 Kommentare
Mengyuan Li
Mengyuan Li am 15 Mai 2021
What version are you running on?
Scott MacKenzie
Scott MacKenzie am 15 Mai 2021
I'm running R2021a. Clearly, Adam's answer is spot on. Good luck.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Adam Danz
Adam Danz am 14 Mai 2021
Bearbeitet: Adam Danz am 14 Mai 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 Kommentare
Mengyuan Li
Mengyuan Li am 15 Mai 2021
Thanks Adam! So I assume TileSpacing = 'none' does not work properly in older versions?
Adam Danz
Adam Danz am 15 Mai 2021
Bearbeitet: Adam Danz am 17 Mai 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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Line Plots finden Sie in Help Center und File Exchange

Produkte


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by