Vertically-stacked subplots cannot be made the same width and to be vertically aligned
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Michael Worthington
am 25 Okt. 2022
Kommentiert: Michael Worthington
am 26 Okt. 2022
I have two subplots: one an image, and one a line plot. I am trying to make it so the two subplots have the same width and are vertically aligned (i.e., have the same distance from the left edge of the window). My understanding of the figure property "Position" is that if you have a figure h, then h.Position returns a vector of the form [left right width height]. I need to make sure then that the "left" and "width" values for each subplot are the same. This is based on the information at https://www.mathworks.com/help/matlab/ref/matlab.ui.figure-properties.html . I am attempting this with the following code:
clc
clear
close all
h1 = subplot(2, 1, 1); % image
imagesc(rand(15, 25))
daspect([1 1 1])
colorbar('southoutside')
set(gca,'xtick',[])
set(gca,'ytick',[])
h1.Units = 'centimeters';
h2 = subplot(2, 1, 2); % line plot
plot(1:25, 1:25)
h2.Units = 'centimeters'; % making sure both subplots have the same units
h2.Position(1) = h1.Position(1); % position of left edge of plot to left
% edge of window should be the same
h2.Position(3) = h1.Position(3); % both subplots should be the same width
h1.Position
h2.Position
The outputs of the last two lines give:
h1.Position = [1.9262 6.4879 11.4829 3.7912]
h2.Position = [1.9262 1.2224 11.4829 3.7912]
The 1st and 3rd value in the Positions of each subplots are the same, so I would expect the subplots to have the same width and to be vertically aligned. However, this is the plot produced:

The subplots are not the same width and the left edges are not aligned. The top subplot should be wider, or at least shifted to the left. I am following the same procedure as I have done previously to succesfully do this, but it is now failing. What am I doing incorrectly? What am I missing?
0 Kommentare
Akzeptierte Antwort
Matt J
am 25 Okt. 2022
Something to do with daspect(). Omitting it gives the intended edge alignment.
Units='normalized';
h1 = subplot(2, 1, 1); % image
imagesc(h1,rand(15, 25))
%daspect([1 1 1])
colorbar('southoutside')
set(gca,'xtick',[])
set(gca,'ytick',[])
h1.Units = Units;
h2 = subplot(2, 1, 2); % line plot
plot(h2, 1:25, 1:25)
h2.Units = Units; % making sure both subplots have the same units
h2.Position([1,3]) = h1.Position([1,3]);
drawnow
2 Kommentare
Matt J
am 25 Okt. 2022
This seems to compensate for whatever daspect is doing:
Units='normalized';
h1 = subplot(2, 1, 1); % image
imagesc(h1,rand(15, 25))
daspect([1 1 1])
colorbar('southoutside')
set(gca,'xtick',[])
set(gca,'ytick',[])
h1.Units = Units;
h2 = subplot(2, 1, 2); % line plot
plot(h2, 1:25, 1:25)
h2.Units = Units; % making sure both subplots have the same units
h2.Position([1,3]) = h1.Position([1,3])+[0.25,-0.5];
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Axis Labels finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

