在 Matlab 中带有颜色条和子图的第三个参数的紧密子图?

Tight subplot with colorbars and subplot's 3rd parameter in Matlab?

我想要一个紧凑的子图,即子图中图形之间的最小间距

我已经在 Matlab 的 FileExchange 中描述了最流行的紧凑子图。 None(etc最流行here Pekka的版本)可以通过下面的代码

data=randi(513,513); 

ax1=subplot(2,1,1); 
plot(mat2gray(pdist(data, 'correlation')));
cbar1=colorbar(ax1);
axis(ax1, 'square');
xlim([0 size(mat2gray(pdist(data, 'correlation')),2)]);
set(cbar1, 'Visible', 'off')

ax2=subplot(2,1,2); 
imshow(squareform( mat2gray(pdist(data, 'correlation')), 'tomatrix') ); 
colormap('parula'); colorbar;
axis(ax2, 'square');  

Pekka 的 tight_subplot 需要没有第三个参数的语法。 它也像示例中那样使用颜色条失败。我不懂为什么。

关于彩条第二个问题的假设

我认为问题可能在于颜色条对象是图形的子对象,而不是轴,并且它们的位置是以标准化图形单位定义的;就像讨论的带注释的对象一样 here。 但是,我不确定如何为此调整紧凑的子图。

作者在 FileExchange 3.3.2016tight_subplot 中编辑后的测试输出

代码

data = randi(513, 513); 

ax1=tight_subplot(2,1,[.01 .03],[.1 .01],[.01 .01]); 
plot(mat2gray(pdist(data, 'correlation')));

ax2=tight_subplot(2,1,[.01 .03],[.1 .01],[.01 .01]); 
imshow(squareform( mat2gray(pdist(data, 'correlation')), 'tomatrix') ); 

你得到

其中 plot 失败并且由于某种原因在第二个数字中有嘈杂的部分。为什么?

扩展 Suever 对 2x2 数字的回答

ax1=axes('OuterPosition', [0 0.5 0.5 0.5]);
plot(u, 'Parent', ax1);
set(ax1, 'XLim', [0, size(u,1)]);
cbar1 = colorbar(); % not needed to assign ax1
set(cbar1, 'Visible', 'off')

ax3 = axes('OuterPosition', [0 0 0.5 0.5]);
image(data, 'Parent', ax3);

D=mat2gray(pdist(pTFD, 'correlation'));

ax2 = axes('OuterPosition', [0.51 0.5 0.5 0.5]);
plot(D, 'Parent', ax2);
set(ax2, 'XLim', [0, size(D,1)])
axis(ax2, 'square');
xlim([0 size(D,2)]);
set(cbar2, 'Visible', 'off')

ax4 = axes('OuterPosition', [0.51 0 0.5 0.5]);
imshow( D_square ); 
axis(ax4, 'square');  

其中 2x2 图形系统和我认为等效的地方


如何使用带有颜色条和第三个参数的 Matlab 紧凑子图?

tight_subplot的第三个参数定义轴对象之间的间隙。对于内置的subplot命令,第三个参数定义了将哪个轴设置为Figure的CurrentAxes。此选项在 tight_subplot 中不可用,因为我个人认为它没有用。通常,我使用返回的轴句柄来指定添加图形的位置。

当您添加 colorbar.

时,现有坐标区对象会重新定位

我在 tight_subplot 中添加了第二个输出参数,它提供轴的输出位置,以便您可以 "reset" 添加颜色条后的轴位置。

[hax, position] = tight_subplot();

% Add a colorbar which alters the positions
colorbar();

% Now reset the positions back to where they were
set(hax, {'Position'}, pos);

与其尝试处理 subplot 和文件交换中的不同版本,我可能只是手动设置轴对象的位置以获得您想要的效果。您可以使用归一化单位,以便位置随着父图形大小的变化而缩放。

此外,您可以设置坐标轴的 OuterPosition 属性 以考虑到正确显示坐标轴的所有文本标签所需的空间。

figure

data=randi(513,513);

set(0, 'defaultaxeslooseinset', [0 0 0 0])

D = mat2gray(pdist(data, 'correlation'));
square = squareform(D, 'tomatrix');

% Set normalized outer position (x,y,width,height)
ax1 = axes('OuterPosition', [0, 0.5, 1, 0.5]);
plot(D, 'Parent', ax1);
set(ax1, 'XLim', [0, size(square, 1)])
axis(ax1, 'square');

cbar1 = colorbar();
set(cbar1, 'Visible', 'off')

% Set normalized outer position (x,y,width,height)
ax2 = axes('OuterPosition', [0 0 1 0.5]);
imshow(square);
colormap('parula'); colorbar;
axis(ax2, 'square');

如果你删除轴上的 x 和 y 刻度

set([ax1,ax2], 'xtick', [], 'ytick', []);

这可以很容易地适应任何维度,类似于以下内容

figure;
% [Rows, Columns]
axdim = [3, 3];

width = 1 ./ axdim(2);
height = 1./ axdim(1);

[x,y] = meshgrid(linspace(0,1,axdim(2)+1), ...
                 linspace(0,1, axdim(1)+1));

for k = 1:numel(x)
    ax = axes('OuterPosition', [x(k), y(k), width, height]);
    set(ax, 'xtick', [], 'ytick', []);
end