Matlab 方块控制中异构矩阵的连接轴?

Linkaxes of Heterogenous Matrixes in Square Control of Matlab?

我试图通过 linkaxes() 链接控制两个具有不同 xlims 和 ylims 的矩阵,其中两个矩阵之间存在反函数。 矩阵(信号D/方阵D_square)的轴分别为ax2/ax4。 轴的尺寸是

ax2的限制与ax4的限制不同,因为linkaxes()只是使所有输入轴具有相同的限制。 图 ax2 的选择(= 缩放图中的区域)应该在图 ax4 中进行适当的选择。 我认为你应该告诉 Matlab 如何去做。 两个数据集的唯一区别是 ax2 的数据是矩阵 D,而 ax4 的数据是矩阵 D_square=squareform(D, 'tomatrix') 进行了逆变换,D=squareform(D_square, 'tomatrix') 所以可以在两个数字之间进行控制。 代码

data=randi(513,513);
D=mat2gray(pdist(data, 'correlation')); 

% Set normalized outer position (x,y,width,height)
ax2 = axes('OuterPosition', [0.51 0.5 0.5 0.5]);
plot(D, 'Parent', ax2);
axis(ax2, 'square');
title('Corr pdist');
cbar2 = colorbar(); 
set(ax2, 'XLim', [0 size(D,2)]);
set(cbar2, 'Visible', 'off')
grid minor;
% Force a draw event to have the axes determine where the
labelconverter = @(x)sprintf('%.2g', x); % 
callback = @(varargin)set(ax2, 'xticklabels', arrayfun(labelconverter, get(ax2, 'xtick'), 'uniform', 0));
set(hFig, 'SizeChangedFcn', callback);
callback(); % necessary for the original small window and its scientific numbering

%% Problem here!
D_square=squareform(D, 'tomatrix');
ax4 = axes('OuterPosition', [0.51 0 0.5 0.5]);
set(ax4, 'XLim', [0 size(D_square,2)]);
image( D_square, 'Parent', ax4 ); % TODO problem here!
set(gca,'YDir','normal');
colormap('parula'); colorbar;
axis(ax4, 'square');
title('Square Corr pdist');

linkaxes([ax2,ax4], 'x');

输出

其中图 ax4 被替换,因为 ax4 的 lims 被 ax2 替换,反之亦然 linkaxes([ax4,ax2], 'x'); 在最后一行。

Suever 的回答中的一些修正

一些变化

代码

data=randi(513,513);
D=mat2gray(pdist(data, 'correlation'));

% Figure out the xrange of your first plot
xrange = [1, numel(D)];

% Set normalized outer position (x,y,width,height)
ax2 = axes('OuterPosition', [0.51 0.5 0.5 0.5]);
plot(D, 'Parent', ax2);
axis(ax2, 'square');
title('Corr pdist');
cbar2 = colorbar(); 
set(ax2, 'XLim', [0 size(D,2)]);
set(cbar2, 'Visible', 'off')
grid minor;
% Force a draw event to have the axes determine where the
labelconverter = @(x)sprintf('%.2g', x); % 
callback = @(varargin)set(ax2, 'xticklabels', arrayfun(labelconverter, get(ax2, 'xtick'), 'uniform', 0));
set(hFig, 'SizeChangedFcn', callback);
callback(); % necessary for the original small window and its scientific numbering

D_square=squareform(D, 'tomatrix');
ax4 = axes('OuterPosition', [0.51 0 0.5 0.5]);
set(ax4, 'XLim', [0 size(D_square,2)]);
him=imagesc( D_square, 'Parent', ax4 ); 
axis(ax4, 'square'); % To let things be square!
set(gca,'YDir','normal');
colormap('parula'); colorbar;
title('Square Corr pdist');

% Set XData AND YData (to keep things square)
set(him, 'XData', xrange, 'YData', xrange); 

linkaxes([ax2,ax4], 'xy');

问题出在哪里

我个人认为这两个数字之间可能无法严格控制(x,y轴都linkaxes([ax2,ax4], 'xy'))。 我认为至少可以控制一个轴(通过 linkaxes([ax2,ax4], 'x')linkaxes([ax2,ax4], 'y')),因为存在反函数。一些经验证据

方形主动控制

此处由 set(him, 'XData', xrange, 'YData', xrange); 主动控制方形。 在原始视图中保持正方形的输出

其中一个完整矩阵。

在原始视图中不保持正方形的输出

禁用set(him, 'XData', xrange, 'YData', xrange);

其中 Fig.ax4 是正确的。否

Suever 第二次编辑成功输出

x 控件中的原始视图由 linkaxes([ax2,ax4], 'x'); 和 Suever 的回答中描述的有关标签的其他更改

中等图片也可以使用。


在使用linkaxes()时如何告诉Matlab图形之间的关系? 请问如何在Matlab中实现两个图形之间的联动控制?

您遇到问题是因为底轴的图像是 513 x 513 (xlims = [0.5 513.5]),而顶轴的 xlimits [0 131328]。如果您需要它们相同,您只需将图像的 XDataYData 更改为与顶部图的 XLims 相同。

% Figure out the xrange of your first plot
xrange = [1, numel(D)];

% Set XData AND YData (to keep things square)
him = image( D_square, 'Parent', ax4 );
set(him, 'XData', xrange, 'YData', xrange); 

现在,当您调用 linkaxes(链接 XLims)时,它们应该一起移动。您还需要调用 axis(ax4, 'tight') 将轴重新拟合到新的图像数据。

我在下面包含了您的代码的修改版本, 使两个图之间的 xticklabels 相同。

hFig = figure();

data = randi(513,513);
D = mat2gray(pdist(data, 'correlation'));
D_square = squareform(D, 'tomatrix');

% Set normalized outer position (x,y,width,height)
ax2 = axes('OuterPosition', [0.51 0.5 0.5 0.5]);
plot(D, 'Parent', ax2);
axis(ax2, 'square');
title('Corr pdist');
cbar2 = colorbar();

set(ax2, 'XLim', [0 size(D,2)]);

set(cbar2, 'Visible', 'off')
grid minor;

% Force a draw event to have the axes determine where the
labelconverter = @(x)sprintf('%.2g', x); % 
callback = @(varargin)set(ax2, 'xticklabels', arrayfun(labelconverter, get(ax2, 'xtick'), 'uniform', 0));
set(hFig, 'SizeChangedFcn', callback);

callback(); % necessary for the original small window and its scientific numbering

ax4 = axes('OuterPosition', [0.51 0 0.5 0.5]);

him = imagesc( D_square, 'Parent', ax4 ); % TODO problem here!

% Set the XData and YData of the image
set(him, 'xdata', [1, size(D, 2)], 'ydata', [1, size(D,2)])
set(ax4,'YDir','normal');
colormap('parula');
colorbar;
axis(ax4, 'square');

% Fit the axes to the new XData and YData
axis(ax4, 'tight')
title('Square Corr pdist');

% Link the two together
linkaxes([ax2,ax4], 'x');

% Ensure that the labels ALSO remain the same
linkprop([ax2,ax4], 'XTickLabel')