python - 为networkx中的不同节点组绘制不同的颜色
问题描述
我正在尝试使用 python3 networkx 模块绘制网络。具体来说,就是为 3 组不同的节点分配颜色。
# visualize DiGraph
def show_plan(self):
G = nx.DiGraph()
G.add_nodes_from(self.nodes)
# add within-district edges to G
for district in self.districts:
G.add_edges_from(self.district_edges[district],
color='black', weight=0.5)
# define default colors and weights to edges of G
colors = nx.get_edge_attributes(G, 'color').values()
weights = nx.get_edge_attributes(G, 'weight').values()
# include cut edges in dotted red style in a separate graph H
H = nx.DiGraph()
H.add_nodes_from(G)
for edge in self.cut_edges:
H.add_edge(*edge, color='r', weight=0.2)
colors_cut = nx.get_edge_attributes(H, 'color').values()
weights_cut = nx.get_edge_attributes(H, 'weight').values()
# possibly assign colors to districts
# draw nodes, node labels, district edges, and cut edges
nx.draw_networkx_nodes(G, self.node_coordinates, label = True,
node_size = 50, node_shape = 'o')
nx.draw_networkx_labels(G, self.node_coordinates, font_size = 4)
nx.draw_networkx_edges(G, self.node_coordinates, edge_color=colors,
width=list(weights), arrows = False,)
nx.draw_networkx_edges(H, self.node_coordinates,
edge_color = colors_cut,
width = list(weights_cut), arrows = False,
style = 'dotted')
# display the finished map
plt.show()
return
具体来说,我想将代码添加到上面写着“#possibly assign colours to districts”的地方
解决方案
推荐阅读
- sql - 插入表时的 SQL 最佳实践/性能。是否使用临时表
- javascript - 在 Javascript 国际象棋引擎 chess.js 中移动排序
- javascript - 带有标记的 Google GeoChart 上的超链接
- java - 在正则表达式java中删除两个单词之间的字符
- node.js - NestJs:根据外部 API 调用返回修改后的响应
- r - 指定自定义主题、序言和包含在使用 R 降价生成的 beamer 演示文稿的 YAML 标头中的路径
- javascript - 如何在 beforeunload 事件上设置我自己的消息
- excel - VBA根据行中第一列的单元格值替换值> 0的变量范围
- python-3.x - GridLayout 在 .kv 文件中无法正常工作
- c++ - C++ __builtin_* 函数源代码