从距离矩阵绘制图形或网络?

2023-12-19

我正在尝试绘制/草绘(matplotlib 或其他 python 库)一个大距离矩阵的 2D 网络,其中距离将是草绘网络的边缘及其节点的线和列。

DistMatrix =
[       'a',   'b',     'c',    'd'],
['a',   0,      0.3,    0.4,    0.7],
['b',   0.3,    0,      0.9,    0.2],
['c',   0.4,    0.9,    0,      0.1],
['d',   0.7,    0.2,    0.1,    0] ]

我正在搜索从这样的(更大:数千列和行)距离矩阵绘制二维网络:节点“a”通过边缘深度 0.3、节点“c”和“d”链接到节点“b” ' 将由 0.1 的边缘深度绑定。 我可以使用哪些工具/库(距离矩阵可以转换为 numpy 矩阵)来获取此类网络的草图/图形投影? (pandas,matplotlib,igraph,...?)和一些线索可以快速做到这一点(我不会定义我自己的 Tkinter 函数来做到这一点;-))? 感谢您收到的答复。


The graphviz http://www.graphviz.org/程序neato tries尊重边缘长度。道格展示了一种方法 https://stackoverflow.com/a/1898456/190597驾驭neato using networkx http://networkx.lanl.gov像这样:

import networkx as nx
import numpy as np
import string

dt = [('len', float)]
A = np.array([(0, 0.3, 0.4, 0.7),
               (0.3, 0, 0.9, 0.2),
               (0.4, 0.9, 0, 0.1),
               (0.7, 0.2, 0.1, 0)
               ])*10
A = A.view(dt)

G = nx.from_numpy_matrix(A)
G = nx.relabel_nodes(G, dict(zip(range(len(G.nodes())),string.ascii_uppercase)))    

G = nx.drawing.nx_agraph.to_agraph(G)

G.node_attr.update(color="red", style="filled")
G.edge_attr.update(color="blue", width="2.0")

G.draw('/tmp/out.png', format='png', prog='neato')

yields


如果您想生成点文件,可以使用

G.draw('/tmp/out.dot', format='dot', prog='neato')

这产生

strict graph {
    graph [bb="0,0,226.19,339.42"];
    node [color=red,
        label="\N",
        style=filled
    ];
    edge [color=blue,
        width=2.0
    ];
    B    [height=0.5,
        pos="27,157.41",
        width=0.75];
    D    [height=0.5,
        pos="69,303.6",
        width=0.75];
    B -- D   [len=2.0,
        pos="32.15,175.34 40.211,203.4 55.721,257.38 63.808,285.53"];
    A    [height=0.5,
        pos="199.19,18",
        width=0.75];
    B -- A   [len=3.0,
        pos="44.458,143.28 77.546,116.49 149.02,58.622 181.94,31.965"];
    C    [height=0.5,
        pos="140.12,321.42",
        width=0.75];
    B -- C   [len=9.0,
        pos="38.469,174.04 60.15,205.48 106.92,273.28 128.62,304.75"];
    D -- A   [len=7.0,
        pos="76.948,286.17 100.19,235.18 167.86,86.729 191.18,35.571"];
    D -- C   [len=1.0,
        pos="94.274,309.94 100.82,311.58 107.88,313.34 114.45,314.99"];
    A -- C   [len=4.0,
        pos="195.67,36.072 185.17,90.039 154.1,249.6 143.62,303.45"];
}

The png然后可以使用以下命令生成文件graphviz neato程序:

neato -Tpng -o /tmp/out.png /tmp/out.dot 
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

从距离矩阵绘制图形或网络? 的相关文章

随机推荐