Vacant
NetworkX Graph显示权重weight

access_time
brush 16个字
whatshot 305 ℃

NetWorkX(图论,用于图的可视化,网络分析)

import networkx as nx
import matplotlib.pyplot as plt

nodes = {
    'start': 'yellow',
    'a': 'green',
    'b': 'green',
    'c': 'green',
    'end': 'yellow'
}

edges = [
    ('start', 'a', 1),
    ('start', 'b', 5),
    ('a', 'b', 4),
    ('a', 'c', 2),
    ('b', 'end', 3),
    ('b', 'c', 4)
]

graph = nx.Graph()
# Add nodes to graph
graph.add_nodes_from(nodes)
# Add edges with weight to graph
graph.add_weighted_edges_from(edges)
# Graph type
pos = nx.spiral_layout(graph)
# Get weight's data and draw labels
edge_label = nx.get_edge_attributes(graph, 'weight')
nx.draw_networkx_edge_labels(graph, pos, edge_labels=edge_label)
nx.draw(graph, with_labels=True, node_color=nodes.values(), pos=pos)
plt.show()

结果图:
1.png

#如无特别声明,该文章均为 Vacant 原创,转载请遵循 署名-非商业性使用 4.0 国际(CC BY-NC 4.0) 协议,即转载请注明文章来源。
#最后编辑时间为: 2021 年 11 月 20 日


create 添加新评论


account_circle
email
language
textsms





关于 DreamCat

主题名称:DreamCat | 版本:X2.6.220211

主题开发:HanFengA7 | TeddyNight | Dev-Leo | CornWorld | WhiteBearcn | DFFZMXJ

Designed by HanFengA7 Power by Typecho

Copyright © 2015-2022 by LychApe All rights reserved!

加我的QQ
加我的微博
加我的支付宝
加我的微信