在Matlab中绘制相关系数图
Drawing graph of correlation coefficients in Matlab
在 Matlab 中,我有一个 n^2 x n^2
矩阵 Corr_M
包含一组 n x n
矩阵 M
的所有条目之间的皮尔逊相关系数,这样 Corr_M(i,j)
是集合中某些 M
的 M(i)
和 M(j)
之间的相关系数。请注意 Corr_M
是对称的。
我想通过在 M
的不同条目之间显示 links 来绘制 Corr_M
图表,其中 M(i)
和 [=16] 之间的 link =] 是彩色的,比方说,如果 Corr_M(i,j)
为正则为红色,如果为负则为蓝色。 link 的粗细表示相关性有多强(最好是 link 相距 0.1,甚至更小,是可区分的)。
对于 3 x 3
M
这可能如下所示:
并非所有条目都会关联,因为其中许多条目不相关(因此相关系数为零不会导致显示 link)。请注意,未显示自相关。诸如在底行中看到的实例,其中天真的实现可能只是将一行放在另一行之上,这是有问题的,但这种天真的实现仍然非常受欢迎。
是否有一种标准的方法可以做到这一点,也许可以使用 Matlab 的一些内置图论函数(不幸的是,我不知道其范围)?
如果没有,我该如何实现?
您可以在 Matlab 中尝试 graph
对象。以下示例假定您的 Corr_M
是一个 n
xn
矩阵(见下文):
% set the source of the lines:
s = repelem(1:n-1,n-1:-1:1);
% set the target of the lines:
t = nonzeros(triu(repmat(2:n,n-1,1)).').';
Corr_M(~Corr_M) = nan; % replace zero weights with nan
weights = nonzeros(tril(Corr_M,-1));
% create the graph object:
G = graph(s,t,weights,n);
% mark the lines to remove from the graph:
threshold = 0.4; % minimum correlation to plot
line_to_remove = isnan(weights) | abs(weights)<threshold;
% remove the lines from the graph:
G = G.rmedge(find(line_to_remove)); %#ok<FNDSB>
% plot it:
p = plot(G); % for labeling the lines uncomment add: 'EdgeLabel',G.Edges.Weight
p.NodeColor = 'k';
% color positive in blue and negative in red:
p.EdgeColor = [G.Edges.Weight<0.' zeros(numel(G.Edges.Weight),1) G.Edges.Weight>0.'];
% set the thickness of the lines:
p.LineWidth = abs(G.Edges.Weight)*5;
axis off
如果您希望节点位于网格中,则需要设置绘制图形的 XData
和 YData
属性。
% get the grid coordinates for all nodes
[x,y] = ndgrid(1:ceil(sqrt(n)),1:ceil(sqrt(n)));
x = x(:);
y = y(:);
% set the nodes in a 'grid' structure
p.XData = x(1:n);
p.YData = y(1:n);
axis ij % flip the plot so it will be orderd like in a matrix
与 n = 9
看起来像这样(使用一些随机的 Corr_M
):
Corr_M =
0 0 0 0 0 0 0 0 0
0.9504 0 0 0 0 0 0 0 0
0.016371 0.24554 0 0 0 0 0 0 0
-0.11467 -0.19375 -0.30812 0 0 0 0 0 0
-0.01241 -0.090871 0.74444 0.34121 0 0 0 0 0
-0.21623 0.36844 0.83935 -0.83914 -0.12302 0 0 0 0
-0.011428 -0.0077929 -0.26243 -0.98249 -0.57997 0.55024 0 0 0
0.64245 -0.6027 0.51424 0.62646 0.32854 0.18052 0.055688 0 0
-0.51699 0.47885 0.44677 0.18128 0.26819 -0.67849 -0.034057 0.28652 0
图表的问题之一是您无法更改的非常小的文本。如果这很重要,请阅读 following suggestion.
在 Matlab 中,我有一个 n^2 x n^2
矩阵 Corr_M
包含一组 n x n
矩阵 M
的所有条目之间的皮尔逊相关系数,这样 Corr_M(i,j)
是集合中某些 M
的 M(i)
和 M(j)
之间的相关系数。请注意 Corr_M
是对称的。
我想通过在 M
的不同条目之间显示 links 来绘制 Corr_M
图表,其中 M(i)
和 [=16] 之间的 link =] 是彩色的,比方说,如果 Corr_M(i,j)
为正则为红色,如果为负则为蓝色。 link 的粗细表示相关性有多强(最好是 link 相距 0.1,甚至更小,是可区分的)。
对于 3 x 3
M
这可能如下所示:
并非所有条目都会关联,因为其中许多条目不相关(因此相关系数为零不会导致显示 link)。请注意,未显示自相关。诸如在底行中看到的实例,其中天真的实现可能只是将一行放在另一行之上,这是有问题的,但这种天真的实现仍然非常受欢迎。
是否有一种标准的方法可以做到这一点,也许可以使用 Matlab 的一些内置图论函数(不幸的是,我不知道其范围)?
如果没有,我该如何实现?
您可以在 Matlab 中尝试 graph
对象。以下示例假定您的 Corr_M
是一个 n
xn
矩阵(见下文):
% set the source of the lines:
s = repelem(1:n-1,n-1:-1:1);
% set the target of the lines:
t = nonzeros(triu(repmat(2:n,n-1,1)).').';
Corr_M(~Corr_M) = nan; % replace zero weights with nan
weights = nonzeros(tril(Corr_M,-1));
% create the graph object:
G = graph(s,t,weights,n);
% mark the lines to remove from the graph:
threshold = 0.4; % minimum correlation to plot
line_to_remove = isnan(weights) | abs(weights)<threshold;
% remove the lines from the graph:
G = G.rmedge(find(line_to_remove)); %#ok<FNDSB>
% plot it:
p = plot(G); % for labeling the lines uncomment add: 'EdgeLabel',G.Edges.Weight
p.NodeColor = 'k';
% color positive in blue and negative in red:
p.EdgeColor = [G.Edges.Weight<0.' zeros(numel(G.Edges.Weight),1) G.Edges.Weight>0.'];
% set the thickness of the lines:
p.LineWidth = abs(G.Edges.Weight)*5;
axis off
如果您希望节点位于网格中,则需要设置绘制图形的 XData
和 YData
属性。
% get the grid coordinates for all nodes
[x,y] = ndgrid(1:ceil(sqrt(n)),1:ceil(sqrt(n)));
x = x(:);
y = y(:);
% set the nodes in a 'grid' structure
p.XData = x(1:n);
p.YData = y(1:n);
axis ij % flip the plot so it will be orderd like in a matrix
与 n = 9
看起来像这样(使用一些随机的 Corr_M
):
Corr_M =
0 0 0 0 0 0 0 0 0
0.9504 0 0 0 0 0 0 0 0
0.016371 0.24554 0 0 0 0 0 0 0
-0.11467 -0.19375 -0.30812 0 0 0 0 0 0
-0.01241 -0.090871 0.74444 0.34121 0 0 0 0 0
-0.21623 0.36844 0.83935 -0.83914 -0.12302 0 0 0 0
-0.011428 -0.0077929 -0.26243 -0.98249 -0.57997 0.55024 0 0 0
0.64245 -0.6027 0.51424 0.62646 0.32854 0.18052 0.055688 0 0
-0.51699 0.47885 0.44677 0.18128 0.26819 -0.67849 -0.034057 0.28652 0
图表的问题之一是您无法更改的非常小的文本。如果这很重要,请阅读 following suggestion.