我有3个物体(一张照片和两个地块)放在一个图上的子图中.它应该如下所示:
但是正如人们可以注意到的那样,照片不应该是正方形而是矩形.我试着这样做(在这里找到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)
我得到的就像这样:
如何强制上图具有照片本身的宽度(而不是照片子图)?设置子图的相等宽度不起作用,因为照片未填充子图区域.
最佳答案 命令轴图像调整图像轴比率.因此,原则上,如果您将两个地块的地积比率调整为相同的比率,它将按您的要求进行.
有一点需要注意;由于您将图形绘制在3×3子图中,而顶部为1×3,右图为3×1,因此图像本质上比图的宽3倍或更高.因此,您必须将图表的x或y比率除以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]); to photo = imagesc([X; X]);给出这个:
请注意,我没有将比率精确地除以3;如果我使用接近4的因子,它才会出来.我不知道为什么会这样; AFAIK,3倍应该可以解决问题……
哦,好吧,至少你现在有办法了:)