我正在尝试使用NetworkX库,但是在使节点显示正确的颜色方面遇到麻烦。以下是我使用颜色图的简单示例。根据我的理解,节点“ B”应显示为红色,但是如图所示,“ C”为红色。有人可以告诉我我做错了吗?非常感谢。
import networkx as nx
import matplotlib
matplotlib.use("agg")
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
failed_patch = mpatches.Patch(color='red', label='failed')
success_patch = mpatches.Patch(color='green', label='success')
plt.legend(handles=[success_patch,failed_patch])
G=nx.DiGraph()
nodes = ['A', 'B', 'C', 'D']
G.add_path(nodes)
color_map = ['forestgreen', 'red', 'forestgreen', 'forestgreen']
lpos = { 'A': (-0.008, 0), 'B': (-0.008, -5), 'C': (-0.008, -10),
'D': (-0.008, -15)}
pos = { 'A': (0, 0), 'B': (0, -5),'C': (0, -10),'D': (0, -15)}
nx.draw(G,node_size=1000,alpha=0.8,pos=pos,node_color=color_map,
node_shape='o',font_size=16, with_labels=True)
plt.savefig('diagram.png')