# How to make sure that the horizontal axes of two figures align?

8 visualizzazioni (ultimi 30 giorni)
min lee il 10 Giu 2024
I have two figures, which I want to put side by side in my thesis.
These two figures have different x and y labels and ticks, therefore, generally their horizontal axes are not on the same height.
That is ugly.
How to make sure the horizontal axes align?
##### 2 CommentiMostra NessunoNascondi Nessuno
Mann Baidi il 10 Giu 2024
Modificato: Mann Baidi il 10 Giu 2024
Hi @min lee, it would be great if you can share a snapshot of the figures or the code executed for plotting the two figures.
min lee il 10 Giu 2024
I have not yet the figures. But the problem is simply like this. The two figures are generated by two different codes and are completely unrelated, so generally the axes are not aligned.

Accedi per commentare.

### Risposte (2)

Star Strider il 10 Giu 2024
If the figures are open at the same time, you can use the linkaxes function. If they are not, you can use xlim one one figure axes (likely the figure with the largest x-value limits) and then use those values on the other. Then, use print or saveas to save the figures individually as images to use in your thesis.
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

Michael Ladegaard il 10 Giu 2024
Modificato: Michael Ladegaard il 10 Giu 2024
It is often a good idea to use figure and/or axes handles.
That way it is easy to make modifications to all axes afterwards in a for loop.
Below is a couple of ideas, which might be helpful.
Use linkaxes to make Matlab decide the YLim or use a loop to define your own limits.
%% Make two figures and two axes
for k = 1:2
% First create figure and axes handles
fig(k) = figure(k);
clf(fig(k))
ax(k) = axes ;
% Plot something (use handles to tell Matlab where to make the plot)
plot(ax(k), magic(16) + 100*k ) % add some offset
% Save YLim info for each plot
myYLimits(k,:) = ax(k).YLim ; % same as: fig(k).Children.YLim ;
end
% Now adjust ylimits according to minimum and maximum limits across all plots
for k = 1:length(fig)
ax(k).YLim = [min(myYLimits,[],"all"), max(myYLimits,[],"all")] ;
end
% Link axes so zoom changes YLim in all figures
%% Alternatively plot everything in same figure (use axes handles to tell Matlab, where to plot the data)
fig3 = figure(3);
clf(fig3)
for k = 1:2
% Make the axes
ax(k) = subplot(1,2,k) ;
% Plot something
plot(ax(k), magic(16) + 100*k ) % add some offset
% Save YLim info for each subplot
myYLimits(k,:) = ax(k).YLim ; % same as: fig3.Children.YLim ;
end
myYLimits = vertcat(fig3.Children.YLim) ; % get YLim from all subplots
for k = 1:length(fig)
fig3.Children(k).YLim = [min(myYLimits,[],"all"), max(myYLimits,[],"all")] ;
end
% Link axes so zoom changes YLim in all subplots
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Categorie

Scopri di più su Labels and Annotations in Help Center e File Exchange

R2023b

### Community Treasure Hunt

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

Start Hunting!

Translated by