网络图在 Python 中不显示沿边的箭头
Network graph not showing arrows along edge in Python
我有一个 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)
能否建议我一种无需修改输入数据(A
和 xy
)即可实现目标的方法?
我设法得到 "arrows"。通过深入研究其他堆栈溢出问题 (here, and 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()
在某些时候,我对 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)
我有一个 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)
能否建议我一种无需修改输入数据(A
和 xy
)即可实现目标的方法?
我设法得到 "arrows"。通过深入研究其他堆栈溢出问题 (here, and 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()
在某些时候,我对 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)