【问题标题】:How do I make a force-directed graph in python?如何在 python 中制作力导向图?
【发布时间】:2016-08-12 23:23:38
【问题描述】:

我想要一个显示几个节点的图表,节点之间的方向箭头表示一种关系,其粗细与它们的连接强度有关。

在 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)

产生:

但在 Python 中,我找不到一个明确的例子。 NetworkX 和 igraph 似乎暗示这是可能的,但我一直无法弄清楚。

【问题讨论】:

    标签: python data-visualization igraph networkx r-qgraph


    【解决方案1】:

    我首先尝试使用 NetworkX 的标准绘图函数来做这个,它使用 matplotlib,但我不是很成功。

    不过,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
    

    (或您的操作系统上的等效项)

    其中显示:

    【讨论】:

      猜你喜欢
      • 2013-02-17
      • 1970-01-01
      • 2018-04-20
      • 1970-01-01
      • 2015-11-15
      • 2013-08-01
      • 2014-06-13
      • 2018-05-10
      • 1970-01-01
      相关资源
      最近更新 更多