首页 文章

网络图形不显示Python中的边缘箭头

提问于
浏览
2

我有一个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 )的情况下,您能否建议我实现目标的方法?

enter image description here

2 回答

  • 4

    我设法得到"arrows" . 从挖掘其他堆栈溢出问题(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()
    

    output

  • 3

    在某些时候,我对网络x绘图设施缺乏适当的箭头支持感到非常恼火,并编写了我自己的,同时保持API几乎相同 . 代码可以找到here .

    enter image description 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)
    

相关问题