首页 文章

Matlab中相关系数的绘制图

提问于
浏览
1

在Matlab中,我有一个 n^2 x n^2 矩阵 Corr_M ,其中包含一组 n x n 矩阵 M 的所有条目之间的Pearson相关系数,这样 Corr_M(i,j) 是集合中某些 MM(i)M(j) 之间的相关系数 . 请注意 Corr_M 是对称的 .

我想通过显示 M 的不同条目之间的链接来绘制 Corr_M ,其中 M(i)M(j) 之间的链接为彩色,例如,如果 Corr_M(i,j) 为正,则为红色;如果为负,则为蓝色 . 链接的厚度表示相关性有多强(优选地,链接间隔0.1,可能甚至更小,可区分) .

对于 3 x 3 M ,这可能如下所示:

enter image description here

并非所有条目都将被连接,因为其中许多条目不相关(因此相关系数为零不会导致显示链接) . 请注意,未显示自相关 . 诸如在底行中看到的实例(其中天真的实现可能只是将一行放在另一行之上)是有问题的,但是这样的天真实现仍然是非常受欢迎的 .

Is there a standard way of doing this, perhaps with some of the in-built graph-theoretic functions of Matlab (which I, unfortunately, do not know the scope of)?

If there isn't, then how can I implement this?

1 回答

  • 2

    您可以在Matlab中尝试 graph 对象 . 以下示例假定您的 Corr_Mn x n 矩阵(请参见下文):

    % 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
    

    如果希望节点位于网格中,则需要设置绘制图形的 XDataYData 属性 .

    % 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
    

    corr_graph

    图形图的一个问题是您无法更改的非常小的文本 . 如果这很重要,请阅读following suggestion .

相关问题