How can I align graphs / graphs in subplots in MATLAB?

I have 3 objects (photo and 2 plots) to put in the subtitles on one figure. It should look like this:

image

But, as you can see, the photograph should not be square, but rectangular. I tried to do it like this (see Matlab here : how to align axes of subtitles when one of them contains a color bar? ):

main=subplot(4,4,[5,6,7,9,10,11,13,14,15]) %photo imagesc(im); axis('image') pion=subplot(4,4,[8,12,16]); %right plot (rotated) view(90, 90) plot(ypion,Ppion,'.k'); poz=subplot(4,4,1:3); %upper plot plot(xpoz,Ppoz,'.k'); pos1=get(poz,'Position') pos2=get(main,'Position') pos3=get(pion,'Position') pos1(3) = pos2(3); %width for the upper plot set(poz,'Position',pos1) pos3(4) = pos2(4); %height for the right plot set(pion,'Position',pos3) 

Everything I get looks like this: image

How can I make the top graph have a width as the photo itself (and not as a substring of the photo)? Setting the equal width of the subtitles does not work, because the photo does not fill the subtitle area.

+4
source share
4 answers

The axis image command adjusts the ratio of the image axis. Thus, in principle, if you adjust the relationship between two graphs in the same ratio, it will do what you want.

There is one caveat; the image is inherently 3 times wider or higher than the graphics, because you built it on a 3x3 subnet, versus 1x3 for the top and 3x1 for the right sections. So you have to divide the x or y ratios of the graphs by 3.

Code example:

 clc, clf % generate some bogus data ypion = rand(500,1); Ppion = 450*rand(500,1); xpoz = rand(500,1); Ppoz = 450*rand(500,1); % Load photo photoSub = subplot(4,4,[5,6,7,9,10,11,13,14,15]); load mandrill photo = imagesc([X,X]); colormap(map) axis image photoAxs = gca; photoAxsRatio = get(photoAxs,'PlotBoxAspectRatio') % right plot subplot(4,4,[8,12,16]); plot(Ppion,ypion,'k.'); rightAxs = gca; axis tight % upper plot subplot(4,4,[1 2 3]); plot(xpoz,Ppoz,'k.'); topAxs = gca; axis tight % adjust ratios topAxsRatio = photoAxsRatio; topAxsRatio(2) = photoAxsRatio(2)/3.8; % NOTE: not exactly 3... set(topAxs,'PlotBoxAspectRatio', topAxsRatio) rightAxsRatio = photoAxsRatio; rightAxsRatio(1) = photoAxsRatio(1)/3.6; % NOTE: not exactly 3... set(rightAxs,'PlotBoxAspectRatio', rightAxsRatio) 

This gives the following result:

Side by side

Just for verification, change photo = imagesc([X,X]); on photo = imagesc([X;X]); gives the following:

over-under

Please note that I did not divide the coefficients by 3 exactly; it came out only OK, if I used factors closer to 4. I do not know why this is so; AFAIK, factor 3 should do the trick ...

Well, at least you have something to work with. :)

+2
source

In this particular case, I suggest using low-level axes directly instead of a high-level subplot .
Use the 'OuterPosition' properties for the three axes created to place them in the right place with the appropriate size.

0
source

If you want the image aligned with the axes (distorted image):

change axis('image') to axis('tight') .

If you want the aspect ratio of the image to remain, and align the axes with the image:

Well, it's quick and dirty, but after applying axis('tight') resize to the appropriate scale ...

0
source

Perhaps the best solution to the accepted answer. This solution is adapted from the original posted here .

 % adjust ratios photoAxsratio = photoAxs.PlotBoxAspectRatio(1)/photoAxs.PlotBoxAspectRatio(2); topAxsratio = photoAxsratio * photoAxs.Position(4)/topAxs.Position(4); topAxs.PlotBoxAspectRatio = [topAxsratio, 1, 1]; rightAxsratio = rightAxs.Position(3) / (photoAxs.Position(3) / photoAxsratio); rightAxs.PlotBoxAspectRatio = [rightAxsratio, 1, 1]; 

Preview:

enter image description here


Little explanation

Some explanations were published in the original post, I will not repeat them here.

The idea is to calculate the correct aspect ratio for the numbers that need to be changed.

The following equations are available:

 Photo.width = Photo.height * Photo.ratio TopAxis.width = TopAxis.height * TopAxis.ratio RightAxis.width = RightAxis.height * RightAxis.ratio 

Let be

 TopAxis.width = Photo.width RightAxis.height = Photo.height 

We have

 TopAxis.height * TopAxis.ratio = Photo.height * Photo.ratio TopAixs.ratio = Photo.ratio * Photo.height / TopAxis.height RightAxis.width / RightAxis.ratio = Photo.width / Photo.ratio RightAxis.ratio = RightAxis.width / (Photo.width / Photo.ratio) 
0
source

All Articles