Matlab的平方控制中异构矩阵的连接轴?
我试图通过Matlab的平方控制中异构矩阵的连接轴?,matlab,data-visualization,Matlab,Data Visualization,我试图通过linkaxes()对两个矩阵进行链接控制,这两个矩阵具有不同的xlims和ylims,其中两个矩阵之间存在一个反函数。 矩阵的轴(信号D/square矩阵D_square)分别为ax2/ax4。 轴的尺寸如下所示 ax2的限制与ax4的限制不同,因为linkaxes()只是使所有输入轴具有相同的限制。 选择图ax2(=缩放图中的区域)应在图ax4中提出适当的选择。 我想你应该告诉Matlab怎么做。 这两个数据集之间的唯一区别是ax2的数据是矩阵D,而ax4的数据是矩阵D_squa
linkaxes()
对两个矩阵进行链接控制,这两个矩阵具有不同的xlims和ylims,其中两个矩阵之间存在一个反函数。
矩阵的轴(信号D
/square矩阵D_square
)分别为ax2
/ax4
。
轴的尺寸如下所示
ax2
的限制与ax4
的限制不同,因为linkaxes()
只是使所有输入轴具有相同的限制。
选择图ax2(=缩放图中的区域)应在图ax4中提出适当的选择。
我想你应该告诉Matlab怎么做。
这两个数据集之间的唯一区别是ax2
的数据是矩阵D
,而ax4
的数据是矩阵D_square=squareform(D,'tomatrix')
,该矩阵具有逆变换,D=squareform(D_square,'tomatrix')
,因此可以在两个图形之间进行控制。
代码
输出
其中,图ax4被替换,因为ax4
的LIM被ax2
替换,反之亦然,链接轴([ax4,ax2],'x')代码>在最后一行
Suever答案中的一些修正
一些变化
- 在
him=imagesc中使用imagesc()代码>
- 通过
轴(ax4,'square')启动/让事物成为正方形代码>紧接着imagesc()
;不知道为什么
- 未知链接轴([ax2,ax4],'xy')代码>或仅一个轴
链接轴([ax2,ax4],'x')代码>
代码
问题在哪里
- 通过设置(him,'XData',xrange,'YData',xrange),使事物保持一致代码>
- 并通过
轴(ax4,'square')启动/使其成直角代码>紧接着imagesc()
;由于某些原因,方形启动距离绘图太远的代码未按预期工作
我个人认为,可能无法通过链接轴([ax2,ax4],'xy')
在两个图形之间进行严格控制(x,y轴)。
我认为只有一个轴(通过linkaxes([ax2,ax4],'x')
或linkaxes([ax2,ax4],'y')
)可以控制,至少因为存在反向函数。一些经验证据
- 在
linkaxes([ax2,ax4],'xy')中输出代码>是两个空数字李>
- 在
linkaxes([ax2,ax4],'x')中输出代码>是一个带有一些图形和完整矩阵的图形。与y控件类似李>
正方形形状的主动控制
这里通过set(him,'XData',xrange,'YData',xrange)对方形进行主动控制代码>。
在原始视图中保持方形的输出
其中有一个完整的矩阵
在原始视图中不保持方形的输出
禁用设置(him,'XData',xrange,'YData',xrange)代码>
其中,图ax4是正确的。没有
Suever第二次编辑的成功输出
通过链接轴([ax2,ax4],“x”)在x控件中显示原始视图代码>和Suever回答中描述的标签的其他更改
其中,媒体图片也具有功能性
在使用linkaxes()时,如何告诉Matlab图形之间的关系?
在Matlab中,如何在两个图形之间建立链接控件?您遇到了问题,因为下轴中的图像是513 x 513(xlims=[0.5 513.5]),而上轴的xlimit[0 131328]。如果需要它们相同,只需将图像的XData
和YData
更改为与顶部打印的XLIM相同即可
% 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')
将轴重新安装到新的图像数据中
我在下面包含了您代码的一个修改版本,它还保持了两个绘图之间的xticklabel相同
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); % http://stackoverflow.com/a/35780915/54964
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')
我想我现在更了解这个问题了。xlims和YLIM在两个矩阵之间是不同的。有一个从D_矩阵
获取D
矩阵的逆函数,因此我们可以在图ax4的linkaxes()中选择图ax2。然而,我不知道该如何告诉Matlab这个关系D_square=squareform(D)
和D=squareform(D_矩阵)
。我认为图像函数应该是imagesc
而不是image
,因为调试中出现了一个奇怪的问题。我成功地调试了一个问题。您需要在两个矩阵中首先启动平方控制。然后,在两个矩阵中控制正方形形状,如您所述。现在,这是手头的主要问题:如何告诉Matlab图ax2中的选择A应该如何匹配图ax4。@Masi“选择”是什么意思?很抱歉混淆了。我的意思是选择缩放图形中的一个区域。@Masi我已经用修改后的代码更新了我的答案。此外,我还无意中使用了imagesc
,根据我的经验,这比image
的痛苦要小一些。
% 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);
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); % http://stackoverflow.com/a/35780915/54964
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')