How can I line up plot edges in subplots?

조회 수: 7 (최근 30일)
dormant
dormant 2023년 3월 24일
댓글: Star Strider 2023년 3월 27일
I want to use subplots to show three orthogonal views of 3D data (latitude,longitude and depth). I need the plots to line up with each other so it looks like it's been "unfolded".
Here is how I tried to do it, with the page size vaguely US letter portrait.
subplot(4,3,[1 2 4 5]);
plot( [Hypo2.lon], [Hypo2.lat], 'r.' );
axis square;
xlim( lonLimits );
ylim( latLimits );
set(gca,'ytick',[]);
subplot(4,3,[3 6]);
plot( [Hypo2.dep], [Hypo2.lat], 'r.' );
xlim( depLimits );
ylim( latLimits );
subplot(4,3,[7 8]);
plot( [Hypo2.lon], [Hypo2.dep], 'r.' );
xlim( lonLimits );
ylim( depLimits );
set( gca, 'YDir', 'reverse' );
And the result:
Is there a way to make the edges of the subplots line up exactly with each other and the two depth axes to be exactly the same size? I suppose I could carefully specify every corner of each plot, but that might make it painful if I want to make changes - such as the subplot going in the bottom row.

채택된 답변

Star Strider
Star Strider 2023년 3월 24일
I was hoping that the 'Position' (actually 'InnerPosition') of the first and last subplot axes would automatically be set correctly, however they return the same values in spite of appearing to be different. The result is that it will be necesary to adjust them manually —
Hypo2.lon = linspace(-62.20, -62.14, 250);
Hypo2.lat = linspace(16.69, 16.77, 250);
Hypo2.dep = [randn(1,numel(Hypo2.lon)); randn(1,numel(Hypo2.lat))];
lonLimits = [min(Hypo2.lon) max(Hypo2.lon)];
latLimits = [min(Hypo2.lat) max(Hypo2.lat)];
depLimits = [min(Hypo2.dep,[],'all') max(Hypo2.dep,[],'all')];
subplot(4,3,[1 2 4 5]);
plot( [Hypo2.lon], [Hypo2.lat], 'r.' );
axis square;
xlim( lonLimits );
ylim( latLimits );
set(gca,'ytick',[]);
Pos = get(gca,'InnerPosition')
Pos = 1×4
0.1300 0.5482 0.4942 0.3768
subplot(4,3,[3 6]);
plot( [Hypo2.dep], [Hypo2.lat], 'r.' );
xlim( depLimits );
ylim( latLimits );
subplot(4,3,[7 8]);
plot( [Hypo2.lon], [Hypo2.dep], 'r.' );
xlim( lonLimits );
ylim( depLimits );
set( gca, 'YDir', 'reverse' );
Pos2 = get(gca,'InnerPosition')
Pos2 = 1×4
0.1300 0.3291 0.4942 0.1577
set(gca,'InnerPosition',[Pos(1)+0.1 Pos2(2) Pos(3)-0.2 Pos2(4)]) % Adjust First & Third Elements Manually
.
  댓글 수: 2
dormant
dormant 2023년 3월 27일
Many thanks.
Star Strider
Star Strider 2023년 3월 27일
As always, my pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Adam Danz
Adam Danz 2023년 3월 24일
편집: Adam Danz 2023년 3월 24일
This is much easier to do using tiledlayout instead of subplot.
Here's a similar demo/template you can use.
x = randn(1,1000);
y = randn(1,1000).*1.5;
figure()
tcl = tiledlayout(3,3); % 3x3 layout
ax1 = nexttile([2,2]); % Consumes the first 2x2 area
plot(ax1, x, y, 'r.')
grid(ax1,'on')
ax2 = nexttile([2,1]); % Consumes the next 2x1 area
histogram(ax2, y, 10, 'orientation', 'horizontal','FaceColor','r')
linkaxes([ax1,ax2],'y')
grid(ax2,'on')
ax3 = nexttile([1,2]); % Consumes the next 1x2 area
histogram(ax3, x, 10, 'FaceColor','r')
ax3.YDir = 'reverse';
linkaxes([ax1,ax3],'x')
grid(ax3,'on')
  댓글 수: 1
dormant
dormant 2023년 3월 27일
Thanks. My first attempt to use tiledlayout didn't work.
I'll try again, but time pressure makes me use the answer from Star Strider.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by