I'm trying to plot/sketch (matplotlib or other python library) a 2D network of a big distance matrix where distances would be the edges of the sketched network and the line and column its nodes.
我正在尝试绘制/绘制(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] ]
I'm searching to sketch/plot the 2d network from such (bigger: thousand of columns and lines) distance matrix: node 'a' is linked to node 'b' by an edge depth of 0.3, nodes 'c' and 'd' would be tied by an edge depth of 0.1. What are the tools/libraries I can used (distance matrix can be converted into numpy matrix) to get the sketch/graphical projection of such network? (pandas, matplotlib, igraph,...?) and some leads to do that quickly (I would not define my self Tkinter function to do that ;-) ) ? thanks for your incoming answers.
我正在寻找从这样的(更大的:数千列和线)距离矩阵绘制/绘制2d网络:节点'a'通过边缘深度0.3,节点'c'和'd链接到节点'b' '将被0.1的边缘深度所束缚。我可以使用哪些工具/库(距离矩阵可以转换为numpy矩阵)来获得这种网络的草图/图形投影? (pandas,matplotlib,igraph,......?)和一些导致快速做到这一点(我不会定义我的自我Tkinter功能来做那个;-))?谢谢你的回答。
2 个解决方案
#1
23
The graphviz program neato
tries to respect edge lengths. doug shows a way to harness neato
using networkx like this:
graphviz程序neato尝试尊重边长。道格显示了一种使用networkx利用neato的方法,如下所示:
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
产量
#2
14
You can use the networkx package, that work perfectly with this kind of problems. Adjust your matrix to remove a simple numpy array like this:
您可以使用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] ])
then import networkx and use it
然后导入networkx并使用它
import networkx as nx
G = G=nx.from_numpy_matrix(DistMatrix)
nx.draw(G)
if you want to draw a weighted version of the graph, you have to specify the color of each edge (at least, I couldn't find a more automated way to do it):
如果你想绘制图形的加权版本,你必须指定每条边的颜色(至少,我找不到更自动化的方法):
nx.draw(G,edge_color = [ i[2]['weight'] for i in G.edges(data=True) ], edge_cmap=cm.winter )
#1
23
The graphviz program neato
tries to respect edge lengths. doug shows a way to harness neato
using networkx like this:
graphviz程序neato尝试尊重边长。道格显示了一种使用networkx利用neato的方法,如下所示:
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
产量
#2
14
You can use the networkx package, that work perfectly with this kind of problems. Adjust your matrix to remove a simple numpy array like this:
您可以使用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] ])
then import networkx and use it
然后导入networkx并使用它
import networkx as nx
G = G=nx.from_numpy_matrix(DistMatrix)
nx.draw(G)
if you want to draw a weighted version of the graph, you have to specify the color of each edge (at least, I couldn't find a more automated way to do it):
如果你想绘制图形的加权版本,你必须指定每条边的颜色(至少,我找不到更自动化的方法):
nx.draw(G,edge_color = [ i[2]['weight'] for i in G.edges(data=True) ], edge_cmap=cm.winter )