How can I align plots/graphics in subplots in MATLAB?

隐身守侯 提交于 2019-12-03 03:41:15

The command axis image adjust the image axis ratio. So, in principle, if you adjust the plot ratios of the two plots to the same ratio, it will do what you want.

There is one caveat; the image is inherently 3 times wider or higher than the plots, due to the fact that you've plotted it in 3x3 subplots, vs 1x3 for the top and 3x1 for the right plots. So, you'll have to divide either the x or y ratios of the plots by 3.

Some example code:

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:

Just to test, changing photo = imagesc([X,X]); to photo = imagesc([X;X]); gives this:

Note that I did not divide the ratios by 3 exactly; it only came out OK if I used factors closer to 4. I do not know why that is; AFAIK, a factor of 3 should do the trick...

Oh well, at least you have something to work with now :)

Anthony

Probably a better solution to the accepted answer. This solution is adapted from the original one 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:


A bit of explanation

Some of the explanation has been posted in the original post, I'm not going to repeat them here.

The idea is to calculate the correct aspect ratio for the figures required to be resized.

We have the following equations:

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

Let

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)

For this particular case I suggest using low-level axes directly instead of high-level subplot.
Use the 'OuterPosition' properties of the three axes objects you create to place them in the right place with the appropriate size.

If you want the image to be aligned to the axes(distorted image):

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

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

Well this is quick and dirty, but after applying axis('tight'), resize the figure to the appropriate scale...

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!