Misalignment plot with several axes

Dear all,
I have the following code that gives me a group of subplots.
for k = 1:N
CellSELpercentil{k} = eval(string(SELtimepercentil(k)));
M=CellSELpercentil{k};
ax1=axes;
subplot(2,3,k,ax1);
imagesc(ax1,km2deg(Xv),km2deg(Yv),M);
h=colorbar(ax1,'southoutside');
ylabel(h,'Sound Exposure Level (dB)');
set(gca,'YDir','normal');
clear title
title(strcat('SEL\_', listofpercentis(k)));
xlabel('LONGITUDE');
ylabel('LATITUDE');
axis tight;
clear title
ax2=axes;
subplot(2,3,k,ax2);
contourf(ax2,km2deg(lon_km),km2deg(lat_km),elevation', [-5 -5],'w','facecolor','w');
ax3=axes;
subplot(2,3,k,ax3);
c2=contourf(ax3,km2deg(lon_km),km2deg(lat_km),elevation', [0 0],'facecolor',[0.5 0.5 0.5]);
linkaxes([ax1,ax2,ax3]);
ax2.Visible = 'off';
ax2.XTick = [];
ax2.YTick = [];
ax3.Visible = 'off';
ax3.XTick = [];
ax3.YTick = [];
ax2.Visible = 'on';
ax2.Visible = 'off';
ax3.Visible = 'on';
ax3.Visible = 'off';
axes(ax1); axis equal;
axes(ax2); axis equal;
axes(ax3); axis equal;
hold on;
ax1.XGrid = 'on';
ax1.YGrid = 'on';
ax1.GridColor = [1, 1, 1]; % [R, G, B]
hold on; contourf(km2deg(lon_km),km2deg(lat_km),elevation', [-50 -50],'w','ShowText','on','facecolor','none'); %linha dos 50m
hold on; contourf(km2deg(lon_km),km2deg(lat_km),elevation', [-100 -100],'w','ShowText','on','facecolor','none'); %linha dos 100m
hold on; contourf(km2deg(lon_km),km2deg(lat_km),elevation', [-250 -250],'w','ShowText','on','facecolor','none'); %linha dos 250m
hold on; contourf(km2deg(lon_km),km2deg(lat_km),elevation', [-1000 -1000],'w','ShowText','on','facecolor','none'); %linha dos 1000m
portugal=text(deg2km(-8.6),deg2km(38.3),'Portugal','FontSize',20); %Portugal Setubal
set(portugal,'Rotation',90);
setubal=text(deg2km(-9.0),deg2km(38.55),'Setubal','FontSize',14); %Portugal Sines
caxis(ax1,[120 180]);
hold off
end
set(gcf,'WindowState','maximized')
However everything works fine, but when I had the colorbar (line 8) the axis kind of get displaced and I get the following plots.
Do you have any idea of how to get it working properly?
Thank you in advance

 Accepted Answer

When you create a colorbar, the figure region is shifted upwards and hence it automatically adjust the positions of ax1.
In order to align the other axis to ax1, add these two lines somewhere after defining ax2 and ax3
ax2.Position = ax1.Position;
ax3.Position = ax1.Position;

5 Comments

Thank you, it solves the problem partially...
When I maximize the window the alignment/or the scale do not follow that maximization.
What should I do now??
In the lower part of your code, try to add the axis name to those lines using contourf (4 lines) and text (2 lines).
Still not working!
I think that the problem is the following line: set(gcf,'WindowState','maximized'); that maximize the window. It only maximize the ax1 and not the rest...
Thank you.
solved!
somehow when I do "set(gcf,'WindowState','maximized')" matlab kind of need time to adapt plot to the maximized window. I add a "pause(1)" and then I can get the job done.
Thank you!
@Ricardo Duarte: See if using drawnow() in place of pause(1) still works. If it does, then you don't have to wait that extra second.

Sign in to comment.

More Answers (0)

Categories

Find more on Graphics Performance in Help Center and File Exchange

Products

Asked:

on 31 Jan 2022

Commented:

on 1 Feb 2022

Community Treasure Hunt

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

Start Hunting!