在Python中,网络图不显示沿边缘的箭头

在Python中,网络图不显示沿边缘的箭头,python,graph,networkx,adjacency-matrix,Python,Graph,Networkx,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

我有一个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)
你能给我一个不修改输入数据(
a
xy
)的方法来实现我的目标吗

我设法得到了“箭头”。通过对其他堆栈溢出问题(和)的深入研究,似乎这是使用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()

在某种程度上,我对networkx绘图工具中缺少适当的箭头支持感到非常恼火,并编写了自己的,同时保持API基本不变。代码可以找到


如果要将标签放在节点顶部,还需要将位置传递到
nx.draw
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)