Clear Filters
Clear Filters

Misalignment plot with several axes

3 views (last 30 days)
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

Simon Chan
Simon Chan on 1 Feb 2022
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
Ricardo Duarte
Ricardo Duarte on 1 Feb 2022
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!
Voss
Voss on 1 Feb 2022
@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 Data Exploration in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!