【问题标题】:NetworkX circular layout increase node spacing and custom positioningNetworkX 圆形布局增加节点间距和自定义定位
【发布时间】:2020-10-05 13:34:02
【问题描述】:

我正在使用 NetworkX 上的 circular layout 图表。 我想:

  1. 增加节点间距。我更改了比例,但节点间距似乎没有变化。
pos = nx.circular_layout(G, scale=2)
  1. 将特定节点并排放置在圆圈中(所有节点的标记不同)。

感谢您的帮助!

【问题讨论】:

    标签: python matplotlib networkx


    【解决方案1】:

    问题的第二部分是比较简单的部分。由于circular_layout 返回的pos 只是一个dict,您可以简单地使用值:

    import networkx as nx
    import matplotlib.pylab as pl
    import random
    
    g = nx.karate_club_graph()
    
    pos = nx.circular_layout(g, )
    node_order = list(g.nodes)
    random.shuffle(node_order)
    changed_pos = dict(zip(node_order, [pos[node] for node in g]))
    

    scale 参数的第一部分有点棘手,scale 参数有效果,但可能不是你想要的:

    pos = nx.circular_layout(g)
    nx.draw(g, pos)
    print(pl.gca().get_xlim())
    # (-1.1, 1.1)
    pl.show()
    
    
    pos = nx.circular_layout(g, scale=200)
    nx.draw(g, pos)
    print(pl.gca().get_xlim())
    # (-220.0, 220.0)
    pl.show()
    # same figure as above
    

    正如in this answer 解释的那样,“节点大小在显示坐标中给出,而节点位置在数据坐标中给出”。 (如果您想在该答案中包含对 netgraph 的引用,这可能会对您有所帮助 - 但我没有使用此软件包的经验。)

    要改变节点之间的间距,可以增大图形大小或减小节点大小:

    pos = nx.circular_layout(g)
    nx.draw(g, pos, node_size=10)
    pl.show()
    
    pl.figure(figsize=(20,20))
    nx.draw(g, pos)
    pl.show()
    

    【讨论】:

      猜你喜欢
      • 2020-05-29
      • 1970-01-01
      • 1970-01-01
      • 2013-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-03
      相关资源
      最近更新 更多