2016-08-12 56 views
1

我想要一個圖表顯示幾個節點,表示關係的節點之間的方向箭頭,以及相對於其連接強度的厚度。如何在python中創建一個強制方向圖?

在R這是非常簡單的

library("qgraph") 
test_edges <- data.frame(
    from = c('a', 'a', 'a', 'b', 'b'), 
    to = c('a', 'b', 'c', 'a', 'c'), 
    thickness = c(1,5,2,2,1)) 
qgraph(test_edges, esize=10, gray=TRUE) 

主要生產: force directed graph via R

但是在Python我一直沒能找到一個明顯的例子。 NetworkX和igraph似乎暗示這是可能的,但我一直無法弄清楚。

回答

2

我首先嚐試了使用matlablotlib的NetworkX標準繪圖函數,但我並不是很成功。

但是,NetworkX也supports drawing to the dot format,其中supports edge weight, as the penwidth attribute

所以這裏是一個解決方案:

import networkx as nx 

G = nx.DiGraph() 
edges = [ 
    ('a', 'a', 1), 
    ('a', 'b', 5), 
    ('a', 'c', 2), 
    ('b', 'a', 2), 
    ('b', 'c', 1), 
    ] 
for (u, v, w) in edges: 
    G.add_edge(u, v, penwidth=w) 

nx.nx_pydot.write_dot(G, '/tmp/graph.dot') 

然後,以顯示圖形,在終端上運行:

dot -Tpng /tmp/graph.dot > /tmp/graph.png 
xdg-open /tmp/graph.png 

(或您的OS上的等價物)

這表明:

output of the graph described by OP

相關問題