【问题标题】:Network graph not showing arrows along edge in Python网络图未在 Python 中沿边缘显示箭头
【发布时间】:2017-09-11 07:10:37
【问题描述】:

我有一个adjacency matrix A 和一个定义每个节点坐标的数组:

import numpy as np
import matplotlib.pyplot as plt
import networkx as nx
%matplotlib inline  

Import adjacency matrix A[i,j]
A = np.matrix([[0, 1, 1, 0, 0, 1, 0],
               [0, 0, 1, 1, 0, 0, 0],
               [0, 0, 0, 1, 1, 1, 0],
               [0, 0, 0, 0, 1, 1, 0],
               [0, 0, 0, 0, 0, 0, 1], 
               [0, 0, 0, 0, 0, 0, 1],
               [0, 0, 0, 0, 0, 0, 0]])

## Import node coordinates
xy = np.array([[0, 0],
               [-20, 20],
               [17, 27], 
               [-6, 49], 
               [15, 65], 
               [-20, 76],
               [5,  100]]) 

我的目标是绘制图表,显示节点之间的连接方式。因此,每条边都应该有一个箭头或双向箭头,指示沿着它前进的方向。

即使我将参数指定为True,我也能够显示连接但没有箭头。

## Draw newtwork
G = nx.from_numpy_matrix(A, xy)
nx.draw_networkx(G, pos=xy, width=3, arrows=True)

您能否建议我一种在不修改输入数据(Axy)的情况下实现目标的方法?

【问题讨论】:

    标签: python graph networkx adjacency-matrix


    【解决方案1】:

    在某些时候,我对 networkx 绘图工具中缺乏适当的箭头支持感到非常恼火,并编写了自己的工具,同时保持 API 几乎相同。代码可以在here找到。

    import numpy as np
    import netgraph
    
    A = np.matrix([[0, 1, 1, 0, 0, 1, 0],
                  [0, 0, 1, 1, 0, 0, 0],
                  [0, 0, 0, 1, 1, 1, 0],
                  [0, 0, 0, 0, 1, 1, 0],
                  [0, 0, 0, 0, 0, 0, 1],
                  [0, 0, 0, 0, 0, 0, 1],
                  [0, 0, 0, 0, 0, 0, 0]])
    
    xy = np.array([[0, 0],
                   [-20, 20],
                   [17, 27],
                   [-6, 49],
                   [15, 65],
                   [-20, 76],
                   [5,  100]])
    
    N = len(A)
    node_labels = dict(zip(range(N), range(N)))
    netgraph.draw(np.array(A), xy / np.float(np.max(xy)), node_labels=node_labels)
    

    【讨论】:

      【解决方案2】:

      我设法得到了“箭”。从挖掘其他堆栈溢出问题(here,here)看来,这是使用 matplotlib 获取箭头的最佳方法。

      import numpy as np
      import networkx as nx
      import matplotlib.pyplot as plt
      
      
      #Import adjacency matrix A[i,j]
      A = np.matrix([[0, 1, 1, 0, 0, 1, 0],
                     [0, 0, 1, 1, 0, 0, 0],
                     [0, 0, 0, 1, 1, 1, 0],
                     [0, 0, 0, 0, 1, 1, 0],
                     [0, 0, 0, 0, 0, 0, 1], 
                     [0, 0, 0, 0, 0, 0, 1],
                     [0, 0, 0, 0, 0, 0, 0]])
      
      ## Import node coordinates
      xy = np.array([[0, 0],
                     [-20, 20],
                     [17, 27], 
                     [-6, 49], 
                     [15, 65], 
                     [-20, 76],
                     [5,  100]]) 
      
      G = nx.from_numpy_matrix(np.array(A), create_using = nx.MultiDiGraph())
      pos = xy
      nx.draw(G,pos)
      labels = {i: i + 1 for i in G.nodes()}
      nx.draw_networkx_labels(G, pos, labels, font_size=15, arrows=True)
      plt.show()
      

      【讨论】:

      • 如果你想要节点顶部的标签,你还需要将位置传递给nx.draw
      猜你喜欢
      • 1970-01-01
      • 2020-02-09
      • 2020-12-16
      • 1970-01-01
      • 1970-01-01
      • 2019-06-19
      • 1970-01-01
      • 2021-11-27
      • 1970-01-01
      相关资源
      最近更新 更多