How can I line up plot edges in subplots?

7 visualizzazioni (ultimi 30 giorni)
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.

Risposta accettata

Star Strider
Star Strider il 24 Mar 2023
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
.

Più risposte (1)

Adam Danz
Adam Danz il 24 Mar 2023
Modificato: Adam Danz il 24 Mar 2023
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 Commento
dormant
dormant il 27 Mar 2023
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.

Accedi per commentare.

Categorie

Scopri di più su 2-D and 3-D Plots in Help Center e File Exchange

Prodotti


Release

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by