Networkx 未在有向图中绘制箭头
Networkx not drawing arrows in directed graph
我有一个 5 节点有向网络,我想使用 networkx
以图形方式表示它。节点应该以这种方式连接:
- 每个节点都用虚线相互连接
- 从文件中读取活动连接并用黑色实心箭头表示
- 进出代码为 运行 的节点(在本例中为节点 1)的活动连接应为红色。
代码可以满足我的要求,但由于某种原因,即使我在想要制作红色边缘时指定了 arrow=True
,箭头也没有出现。由于某种原因,它似乎忽略了我在 edge_color
之后指定的每个选项。
我注意到当我在所有节点之间绘制边时,如果我没有指定 arrows=False
它会忽略 style='dashed', alpha=0.5
并且只绘制一条灰色实线。这与问题有什么关系吗?
这是我的代码,有问题的部分用注释注释:
import networkx as nx
import matplotlib.pyplot as plt
class Graph(nx.DiGraph):
def __init__(self, *args, **kwargs):
self.nodeNum = 1
self.pos = {'1': (0, 10),
'2': (10, 3),
'3': (6, -8),
'4': (-6, -8),
'5': (-10, 3)}
self.allConns = []
for i in range(5):
for j in range(i, 5):
self.allConns.append((str(i+1), str(j+1)))
super().__init__(*args, **kwargs)
def read_connections(self):
filename = 'PATH/TO/FILE'
with open(filename) as f:
self.connList = []
self.nodeConns = []
for line in f:
if line[-2] == '1':
self.connList.append((str(line[0]), str(line[1])))
if line[0] == self.nodeNum or line[1] == self.nodeNum:
self.nodeConns.append((str(line[0]), str(line[1])))
else:
pass
else:
pass
def generate_graph(self):
self.read_connections()
self.add_nodes_from(self.pos.keys())
self.add_edges_from(self.connList)
def draw_graph(self, fig=None, ax=None, show=False):
if fig == None:
fig = plt.figure()
if ax == None:
ax = fig.add_subplot(111)
ax.axis('off')
options = {'node_color': 'cyan',
'node_size': 1500,
'width': 2,
'arrowstyle': '-|>',
'arrowsize': 14,
'font_size': 20
}
# This works, but if I do not specify arrows=False it draws solid lines with no transparency
nx.draw_networkx_edges(self, self.pos, ax=ax, edgelist=self.allConns, edge_color='grey',
style='dashed', alpha=0.5, arrows=False)
# This is the main part which works. It connects the nodes from the file with black arrows
nx.draw_networkx(self, self.pos, arrows=True, ax=ax, **options)
# This is the part that gives me problems. It seems to ignore every option after edge_color
nx.draw_networkx_edges(self, self.pos, ax=ax, edgelist=self.nodeConns, edge_color='r',
style='solid',alpha=1,
arrows=True, width=3, arrowsize=14, arrowstyle='-|>')
nx.draw_networkx_nodes(self, self.pos, ax=ax, nodelist=[self.nodeNum], node_color='r',
node_size=1500)
fig.tight_layout()
if show == True:
fig.show()
if __name__ == '__main__':
graph = Graph()
graph.generate_graph()
graph.draw_graph(show=True)
以及结果示例。请注意红色边缘没有箭头。
我很确定绘制了箭头,但根本看不到,因为您之后默认绘制具有更大节点大小的节点。把 node_size
给 draw_networkx_edges
应该可以解决你的问题
nx.draw_networkx_edges(self, self.pos, ax=ax,
edgelist=self.nodeConns, edge_color='r',
style='solid',alpha=1, arrows=True, width=3,
arrowsize=14, arrowstyle='-|>',
node_size=1500,
)
draw_networkx_edges
的文档也包含以下注释
For directed graphs, arrows are drawn at the head end. (...) Be sure to include node_size as a keyword argument; arrows are drawn considering the size of nodes
我有一个 5 节点有向网络,我想使用 networkx
以图形方式表示它。节点应该以这种方式连接:
- 每个节点都用虚线相互连接
- 从文件中读取活动连接并用黑色实心箭头表示
- 进出代码为 运行 的节点(在本例中为节点 1)的活动连接应为红色。
代码可以满足我的要求,但由于某种原因,即使我在想要制作红色边缘时指定了 arrow=True
,箭头也没有出现。由于某种原因,它似乎忽略了我在 edge_color
之后指定的每个选项。
我注意到当我在所有节点之间绘制边时,如果我没有指定 arrows=False
它会忽略 style='dashed', alpha=0.5
并且只绘制一条灰色实线。这与问题有什么关系吗?
这是我的代码,有问题的部分用注释注释:
import networkx as nx
import matplotlib.pyplot as plt
class Graph(nx.DiGraph):
def __init__(self, *args, **kwargs):
self.nodeNum = 1
self.pos = {'1': (0, 10),
'2': (10, 3),
'3': (6, -8),
'4': (-6, -8),
'5': (-10, 3)}
self.allConns = []
for i in range(5):
for j in range(i, 5):
self.allConns.append((str(i+1), str(j+1)))
super().__init__(*args, **kwargs)
def read_connections(self):
filename = 'PATH/TO/FILE'
with open(filename) as f:
self.connList = []
self.nodeConns = []
for line in f:
if line[-2] == '1':
self.connList.append((str(line[0]), str(line[1])))
if line[0] == self.nodeNum or line[1] == self.nodeNum:
self.nodeConns.append((str(line[0]), str(line[1])))
else:
pass
else:
pass
def generate_graph(self):
self.read_connections()
self.add_nodes_from(self.pos.keys())
self.add_edges_from(self.connList)
def draw_graph(self, fig=None, ax=None, show=False):
if fig == None:
fig = plt.figure()
if ax == None:
ax = fig.add_subplot(111)
ax.axis('off')
options = {'node_color': 'cyan',
'node_size': 1500,
'width': 2,
'arrowstyle': '-|>',
'arrowsize': 14,
'font_size': 20
}
# This works, but if I do not specify arrows=False it draws solid lines with no transparency
nx.draw_networkx_edges(self, self.pos, ax=ax, edgelist=self.allConns, edge_color='grey',
style='dashed', alpha=0.5, arrows=False)
# This is the main part which works. It connects the nodes from the file with black arrows
nx.draw_networkx(self, self.pos, arrows=True, ax=ax, **options)
# This is the part that gives me problems. It seems to ignore every option after edge_color
nx.draw_networkx_edges(self, self.pos, ax=ax, edgelist=self.nodeConns, edge_color='r',
style='solid',alpha=1,
arrows=True, width=3, arrowsize=14, arrowstyle='-|>')
nx.draw_networkx_nodes(self, self.pos, ax=ax, nodelist=[self.nodeNum], node_color='r',
node_size=1500)
fig.tight_layout()
if show == True:
fig.show()
if __name__ == '__main__':
graph = Graph()
graph.generate_graph()
graph.draw_graph(show=True)
以及结果示例。请注意红色边缘没有箭头。
我很确定绘制了箭头,但根本看不到,因为您之后默认绘制具有更大节点大小的节点。把 node_size
给 draw_networkx_edges
应该可以解决你的问题
nx.draw_networkx_edges(self, self.pos, ax=ax,
edgelist=self.nodeConns, edge_color='r',
style='solid',alpha=1, arrows=True, width=3,
arrowsize=14, arrowstyle='-|>',
node_size=1500,
)
draw_networkx_edges
的文档也包含以下注释
For directed graphs, arrows are drawn at the head end. (...) Be sure to include node_size as a keyword argument; arrows are drawn considering the size of nodes