sol*_*sol 22 python plot graph 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, 0.1, 0] ]
Run Code Online (Sandbox Code Playgroud)
我正在寻找从这样的(更大的:数千列和线)距离矩阵绘制/绘制2d网络:节点'a'通过边缘深度0.3,节点'c'和'd链接到节点'b' '将被边缘深度0.1绑定.我可以使用哪些工具/库(距离矩阵可以转换成numpy矩阵)来获得这种网络的草图/图形投影?(pandas,matplotlib,igraph,......?)和一些导致快速做到这一点(我不会定义我的自我Tkinter功能来做那个;-))?谢谢你的回答.
unu*_*tbu 28
的graphviz的程序neato 试图尊重边缘长度.道格显示了一种neato利用networkx进行利用的方法,如下所示:
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')
Run Code Online (Sandbox Code Playgroud)
产量

如果要生成点文件,可以使用
G.draw('/tmp/out.dot', format='dot', prog='neato')
Run Code Online (Sandbox Code Playgroud)
产量
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"];
}
Run Code Online (Sandbox Code Playgroud)
png然后可以使用该graphviz neato程序生成该文件:
neato -Tpng -o /tmp/out.png /tmp/out.dot
Run Code Online (Sandbox Code Playgroud)
Enr*_*eri 16
您可以使用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] ])
Run Code Online (Sandbox Code Playgroud)
然后导入networkx并使用它
import networkx as nx
G = G=nx.from_numpy_matrix(DistMatrix)
nx.draw(G)
Run Code Online (Sandbox Code Playgroud)
如果你想绘制图形的加权版本,你必须指定每条边的颜色(至少,我找不到更自动化的方法):
nx.draw(G,edge_color = [ i[2]['weight'] for i in G.edges(data=True) ], edge_cmap=cm.winter )
Run Code Online (Sandbox Code Playgroud)