如何在 Matlab 中使用 Position 绘制 subplot + imagesc?

How to subplot + imagesc with a Position in Matlab?

情况:使用 imagesc 更改单个子图的位置

%% Matlab recommends this structure if axes(); in loop
a1 = subplot(1,2,1); 
a2 = subplot(1,2,2); 
while 1    
    plot(a1, rand(3))    
    plot(a2, rand(3))    
    drawnow 
end

%% Test code
unitsPerInches=[0 0 15 15];
figure('Units', 'inches');
a1 = subplot(1,2,1); 
a2 = subplot(1,2,2); 
while 1    
    set(a1, 'Position', unitsPerInches); % TODO how to affect a1's Position here only? 
    imagesc(a1, rand(3))    
    imagesc(a2, rand(3))    
    drawnow 
end

打开

  1. imagescplot(a1,rand(3)) 对应的结构是什么?
  2. 如何在循环内改变Position of figure

第一季度前进 - 几乎完成

%% Extension to imagesc
figure
a1=subplot(1,2,1);
a2=subplot(1,2,2);
for counter=1:2;
    imagesc(a1,rand(3))
    imagesc(a2,rand(3))
    drawnow    
end

图1 Docs 示例的输出,图 2 Imagesc 的输出,图 3 关于 Q2,其中 Position 影响两个子图

Q1 快完成了;我刚刚忘记了如何在 imagesc 中获得相应的情节; x 值应该放在那里但伪代码 imagesc(a1,XDATA,rand(3)) 不成功。

第二季度落后

代码

%% Extension to imagesc
unitsPerInches=[0 0 15 15];
figure
a1=subplot(1,2,1);
a2=subplot(1,2,2);
for counter=1:2;
    set(a1, 'Position', unitsPerInches); % TODO how to affect a1's Position here only?
    imagesc(a1,rand(3))
    imagesc(a1,rand(3))
    drawnow    
end

输出:位置影响图 3 中的两个图像。

我想我误解了这里的 Position 的意思,因为输出太奇怪了。

测试第 2 季度的 EBH

当有两个图的子图时,隐式分配会导致问题

unitsPerInches=[0 0 15 15];
aFig=figure();
a1=subplot(1,2,1);
a2=subplot(1,2,2);

bFig=figure();
b1=subplot(1,2,1);
b2=subplot(1,2,2);

for counter=1:2;
    if counter==1
        set(a1, 'Position', unitsPerInches); % affect only position of a1
    end
    subplot(1,2,counter);    
    imagesc(rand(3));
    drawnow    

    subplot(1,2,counter);    
    imagesc(rand(3));
    drawnow    
end

输出:子图的第二个数字失败。

系统:Linux Ubuntu 16.04 64 位
Linux 内核 4.6
Matlab: 2016a
相关主题:Matlab7 'bug' when using "subplot" + "imagesc"?

我不是 100% 确定你在问什么,但我认为这是关于在循环中组合多个 imagesc 语句。我会做一些更直接的事情——使用 gca 并将子图放在循环中。通常,如果您想以编程方式处理多个图像,将它们放在某种结构中而不是创建许多不同名称的变量是有意义的。另请注意,while 1 可能并不是您真正想要的——它会影响您的图形设备驱动程序——并且 pause 可以接受一个参数作为等待函数,持续几分之一秒如果需要的话。

testImages{1}=double(imread('coins.png')); 
testImages{2}=double(imread('cameraman.tif')); 

h=figure; 
set(h,'color','w'); %This handle refers to the background window 


for ix=1:2 
     subplot(1,2,ix); 
     imagesc(testImages{ix}); 
     axis equal;
     colormap gray; 

     %Change, for example, axis position
     curPoss=get(gca,'Position'); %gca stands for 'get current axis'
     set(gca,'Position',curPoss+1e-2*ix^2); %Move one image up a bit
end

有帮助吗?

如果你想在图形之间跳转,制作一个数组,并在循环中使用它:

unitsPerInches = [0.1 0.1 0.15 0.15];
figs = [figure(1) figure(2)];

for f = 1:numel(figs)
    figure(figs(f));
    for counter = 1:2;
        subplot(1,2,counter);
        imagesc(rand(3));
        drawnow
    end
    figs(f).Children(1).Position = unitsPerInches;
    figs(f).Children(2).Position = unitsPerInches+0.3;
end

unitsPerInches 的原始值是错误的,因为 'Position' property of an axes takes values between 0 to 1 by default. You can change this using the 'Units' 属性,例如:

figs(f).Children(1).Units = 'inches';

此示例的输出是两个如下所示的数字:

左下角有一个小轴,右上角有一个大轴。

所以,回到你原来的问题:

  1. What is imagesc corresponding structure to plot(a1,rand(3))?

不是将坐标轴传递给 imagesc,而是将焦点设置在相关图形上,并使用以下子图:

figure(h)
subplot(x,y,c)
imagesc(data)

其中 h 是相关图形的句柄,c 是子图在 h 中要绘制图像的位置(1 到 [ 之间的数字=25=]), 在这两行之后调用 imagesc.

  1. How to change 'Position' of figure inside the loop?

在这个问题中不清楚是否要更改figure or the axes的'Position',它们的单位和含义不同,但两者的访问方式相同:

h.Position = [left bottom width height]; % for the position of the figure
h.Children(c).Position = [left bottom width height]; % for the position of the axes

其中 h 和以前一样,但是 c 可能编号不同,因此 subplot(x,y,c) 可能与 h.Children(c) 指的不是同一个轴。但是,您始终可以使用 gca 来获取当前轴的句柄:

ax = gca;
ax.Position = [left bottom width height];

希望现在一切都清楚了,如果还有其他问题,请告诉我 ;)