How to create several figures in a subplot with different axes

6 次查看(过去 30 天)
Dear all,
I'm trying to create a subplot with 7lines and 1 column that plot all of my results consecutivelly.
I have this piece of code:
subplot(7,1,k);
ax1=axes;
imagesc(ax1,Xv,Yv,M);
set(gca,'YDir','normal');
h=colorbar;
ylabel(h,'TOTAL Sound Pressure Level (dB)');
clear title
title(strcat('SPL Broadband\_', listofpercentis(k)));
xlabel('LONGITUDE');
ylabel('LATITUDE');
axis tight;
clear title
ax2=axes;
contourf(ax2,lon_km,lat_km,elevation', [0 0]);
linkaxes([ax1,ax2]);
ax2.Visible = 'off';
ax2.XTick = [];
ax2.YTick = [];
set([ax1,ax2],'Position',[.17 .11 .685 .815]);
ax2.Visible = 'on';
ax2.Visible = 'off';
cb1 = colorbar(ax1,'Position',[.865 .112 .04 .815]);
ylabel(cb1, 'SPL (dB)','FontSize',12);
%AUTOMATIC WAY
% xlim([ax1,ax2],[xmin xmax]);
% ylim([ax1,ax2],[ymin ymax]);
axes(ax1); axis equal;
axes(ax2); axis equal;
hold on;
%plot(km2deg(Sx),km2deg(Sy), '*k')
%plot(-9,37.96, 'Ok');
%source=text(deg2km(-8.99),deg2km(37.94),'source','FontSize',12)
portugal=text(deg2km(-8.65),deg2km(37.86),'Portugal','FontSize',20);
set(portugal,'Rotation',90);
sines=text(deg2km(-8.869),deg2km(37.96),'*Sines','FontSize',14);
caxis(ax1,[0 200]);
The problem is that all plots are displayed in the same position and I want to display the first plot in the first line, the second plot in the second line, etc etc.
Thanks in advance,

回答(2 个)

Scott MacKenzie
Scott MacKenzie 2021-6-9
You're aren't using subplot correctly. For each plot you need a separate call to subplot with the index (in your case, row) where you want the plot to appear:
subplot(7,1,1);
plot ... % something in the 1st row
subplot(7,1,2);
plot ... % something in the 2nd row
subplot(7,1,3);
plot ... % something in the 3rd row
...
Type doc subplot in the MATLAB's command window for more explanation and examples

Ricardo Duarte
Ricardo Duarte 2021-6-9
I overcame this by doing the following code:
ax1=axes;
subplot(7,1,k,ax1);
imagesc(ax1,Xv,Yv,M);
set(gca,'YDir','normal');
clear title
title(strcat('SPL Broadband\_', listofpercentis(k)));
xlabel('LONGITUDE');
ylabel('LATITUDE');
axis tight;
clear title
ax2=axes;
subplot(7,1,k,ax2);
contourf(ax2,lon_km,lat_km,elevation', [0 0]);
linkaxes([ax1,ax2]);
ax2.Visible = 'off';
ax2.XTick = [];
ax2.YTick = [];
set([ax1,ax2],'Position',[.17 .11 .685 .815]);
ax2.Visible = 'on';
ax2.Visible = 'off';
%AUTOMATIC WAY
% xlim([ax1,ax2],[xmin xmax]);
% ylim([ax1,ax2],[ymin ymax]);
axes(ax1); axis equal;
axes(ax2); axis equal;
hold on;
%plot(km2deg(Sx),km2deg(Sy), '*k')
%plot(-9,37.96, 'Ok');
%source=text(deg2km(-8.99),deg2km(37.94),'source','FontSize',12)
portugal=text(deg2km(-8.65),deg2km(37.86),'Portugal','FontSize',20);
set(portugal,'Rotation',90);
sines=text(deg2km(-8.869),deg2km(37.96),'*Sines','FontSize',14);
caxis(ax1,[0 200]);
hold off
end
the thing is, now the land part of the figure is not in the correct position (see the figure)

类别

Help CenterFile Exchange 中查找有关 2-D and 3-D Plots 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by