【问题标题】:How to subplot + imagesc with a Position in Matlab?如何在 Matlab 中使用位置子图 + imagesc?
【发布时间】:2016-08-03 20:30:10
【问题描述】:

情况:用 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. 如何在循环内更改Positionfigure

在第一季度向前推进 - 几乎完成

%% 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,其中位置影响两个子图

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 的含义,因为输出太奇怪了。

在第二季度测试 EBH 的 proposal

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

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"?

【问题讨论】:

标签: matlab image-processing matlab-figure subplot


【解决方案1】:

我不是 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

这有帮助吗?

【讨论】:

  • 我不完全确定将子图放在循环中,因为 Matlab 的文档说你不应该这样做;等等,如果你正在使用axes() 函数。在循环中包含subplot 会占用大量资源。 - - 另外,您只有一个图形对象。它可能是正确的,不确定。假设您希望在子图中的两个图中有不同的unitsPerInches。如何处理?
  • 我在模拟中使用了类似的方法,显示 2 imagesc 在不同的子图中不断刷新,因此我必须在循环中一直在它们之间“移动”,它只是很好。
  • 请看正文。预分配也适用于imagesc。现在,只有 Q2 是位置的问题。我想我误解了它的意思。
【解决方案2】:

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

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 的原始值是错误的,因为 axes'Position' 属性默认采用 0 到 1 之间的值。您可以使用 'Units' 属性更改此设置,例如:

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

此示例的输出是两个图形,如下所示:

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

那么,回到你原来的问题:

  1. imagescplot(a1,rand(3))对应的结构是什么?

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

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

其中h 是相关图形的句柄,ch 中要绘制图像的子图位置(1 到x*y 之间的数字),在这两个之后您拨打imagesc的线路。

  1. 如何更改循环内图形的'Position'

在这个问题中,不清楚是否要更改figureaxes 的“位置”,它们具有不同的单位和含义,但都可以通过相同的方式访问:

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];

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

【讨论】:

  • @Masi 看看我对答案的编辑。让我知道是否有任何东西未打开;)
  • 假设每个图都有不同数量的子图。你能像 figs 那样循环每个图形的子图吗?
  • (1) figure('Units', 'inches', 'Position', unitsPerInches); 中的语法是正确的,虽然我不知道你试图画什么,所以我不知道这是否是这样做的方式。 (2) 您可以在子图上循环,但实现方式取决于您的循环的外观。例如,您可以设置计数器以迭代不同数量的子图(如果您不知道有多少子图,您可以使用numel(hFig.Children) 来获取它们的数字),还有一些其他方法可用,但最优雅快速的方法取决于您的需要。
  • @Masi,如果没有任何问题,您可以将此问题标记为“已回答”吗?
  • 我尽快做。我想在我的系统中彻底测试它。我的系统中目前存在以下情况,这阻止了我继续进行github.com/altmany/export_fig/issues/171export_fig 功能在更改后不稳定I,我不明白为什么会这样。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-27
  • 1970-01-01
  • 2013-05-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多