How can I line up plot edges in subplots?
7 views (last 30 days)
Show older comments
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.
0 Comments
Accepted Answer
Star Strider
on 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')
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')
set(gca,'InnerPosition',[Pos(1)+0.1 Pos2(2) Pos(3)-0.2 Pos2(4)]) % Adjust First & Third Elements Manually
.
2 Comments
More Answers (1)
Adam Danz
on 24 Mar 2023
Edited: Adam Danz
on 24 Mar 2023
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')
See Also
Categories
Find more on 2-D and 3-D Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!