Python 从距离矩阵绘制图形或网络?
我正在尝试绘制/绘制(matplotlib或其他python库)一个大距离矩阵的2D网络,其中距离是绘制网络的边,直线和柱是其节点Python 从距离矩阵绘制图形或网络?,python,graph,plot,social-networking,Python,Graph,Plot,Social Networking,我正在尝试绘制/绘制(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
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] ]
我正在搜索从这样的距离矩阵(更大:数千列和数千行)绘制/绘制2d网络:节点“a”与节点“b”的连接深度为0.3,节点“c”和“d”的连接深度为0.1。
我可以使用哪些工具/库(距离矩阵可以转换为numpy矩阵)来获得此类网络的草图/图形投影?(pandas、matplotlib、igraph等)和一些快速实现这一点的线索(我不会定义我自己的Tkinter函数来实现这一点;-)?
感谢您的回答。您可以使用networkx软件包,它可以完美地解决此类问题。 调整矩阵以删除简单的numpy数组,如下所示:
DistMatrix =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] ])
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')
然后导入networkx并使用它
import networkx as nx
G = G=nx.from_numpy_matrix(DistMatrix)
nx.draw(G)
如果要绘制图形的加权版本,必须指定每条边的颜色(至少,我找不到更自动化的方法):
程序neato
尝试尊重边长度。要使用以下方法来控制neato
:
DistMatrix =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] ])
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')
屈服
如果要生成点文件,可以使用
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"];
}
然后可以使用graphviz
neato
程序生成png
文件:
neato -Tpng -o /tmp/out.png /tmp/out.dot
理论上,这对于某些距离矩阵是不可能的。例如,想象一个4 x 4的距离矩阵,所有条目为1。这定义了一个三维单纯形。无法将此图等距嵌入到二维中。在这种情况下,程序应该怎么做?对,所以没有“边长度”而是“连接两个节点的边深度”。我已经尝试了您建议的代码,适合我的需要(删除A.view)它甚至对7个节点都不起作用。G是正确的。可能出了什么问题?我使用的是graphviz 2.36。在这种情况下,我的错误
'module'对象没有属性“to\u agraph”
。为了修复这个问题,我使用了nx.drawing.nx\u agraph.to\u agraph
如何在没有python的情况下在纯graphviz中绘制相同的图形?@Snochacz:我更新了回答包括networkx生成的点文件。我无法安装pygraphviz。是否可以使用neato并使用graphviz包编写相同的算法(使用pydot)?什么是cm.winter
?