networkx怎么显示图_Networkx:如何在图形图中显示节点和边的属性

I have a graph G with attribute 'state' for nodes and edges. I want to draw the graph, all nodes labelled, and with the state marked outside the corresponding edge/node.

for v in G.nodes():

G.node[v]['state']='X'

G.node[1]['state']='Y'

G.node[2]['state']='Y'

for n in G.edges_iter():

G.edge[n[0]][n[1]]['state']='X'

G.edge[2][3]['state']='Y'

The command draw.networkx has an option for labels, but I do not understand how to provide the attribute as a label to this command. Could someone help me out?

解决方案

It's not so pretty - but it works like this:

from matplotlib import pyplot as plt

import networkx as nx

G = nx.Graph()

G.add_edge(1,2)

G.add_edge(2,3)

for v in G.nodes():

G.node[v]['state']='X'

G.node[1]['state']='Y'

G.node[2]['state']='Y'

for n in G.edges_iter():

G.edge[n[0]][n[1]]['state']='X'

G.edge[2][3]['state']='Y'

pos = nx.spring_layout(G)

nx.draw(G, pos)

node_labels = nx.get_node_attributes(G,'state')

nx.draw_networkx_labels(G, pos, labels = node_labels)

edge_labels = nx.get_edge_attributes(G,'state')

nx.draw_networkx_edge_labels(G, pos, labels = edge_labels)

plt.savefig('this.png')

plt.show()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值