如何在MATLAB的子图中对齐图形/图形? [英] How can I align plots/graphics in subplots in MATLAB?

查看:1092
本文介绍了如何在MATLAB的子图中对齐图形/图形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个对象(一张照片和2个地块)可以放在一个图形上的子图中.它应该看起来像这样:

I have 3 objects (a photo and 2 plots) to put into subplots on one figure. It should look like this:

但是请注意,照片不应是正方形,而应是矩形.我试图以这种方式进行操作(在此处找到

But as one can notice, the photo should not be square but rectangle. I tried to make it this way (found here Matlab: How to align the axes of subplots when one of them contains a colorbar?):

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) 

我所得到的是这样的:

All I get is like this:

如何强制上部图的宽度作为照片本身(而不是照片子图)?设置子图的相等宽度是行不通的,因为照片没有填充子图区域.

How can I force the upper plot to have the width as the photo itself (not as the photo subplot)? Setting the equal widths of the subplots doesn't work, as the photo doesn't fill the subplot area.

推荐答案

命令axis image调整图像轴比率.因此,原则上,如果将两个地块的地积比率调整为相同的比率,它将完成您想要的操作.

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.

有一个警告:由于您已将其绘制在3x3子图中,而顶部是1x3,右侧是3x1,因此该图像本质上比图宽或高3倍.因此,您必须将图的xy比率除以3.

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.

一些示例代码:

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)

这将产生以下结果:

只需测试,将photo = imagesc([X,X]);更改为photo = imagesc([X;X]);即可得到:

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

请注意,我没有将比率精确地除以3.仅当我使用接近4的因子时,结果才可以.我不知道为什么. AFAIK,系数为3应该可以解决问题...

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 :)

这篇关于如何在MATLAB的子图中对齐图形/图形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆