简体   繁体   English

Python networkx 图标签

[英]Python networkx graph labels

I have two data frames that I am using to create a graph with networkx in Python.我有两个数据框,用于在 Python 中使用 networkx 创建图形。 The dataframe df1(node coordinates) and df2(edge info), look as such:数据帧 df1(节点坐标)和 df2(边缘信息)如下所示:

    location     x      y
0   The Wall     145    570
2   White Harbor 140    480

and

    location    x             y 
56  The Wall    Winterfell    259 
57  Winterfell  White Harbor  247 

This is the code I implemented to try and graph it:这是我为尝试绘制它而实现的代码:

plt.figure()
G=nx.Graph()

for i, x in enumerate(df1['location']):
  G.add_node(x, pos=(df1['x'][i], df1['y'][i]))

for x, x2, w in zip(df2['location'], df2['x'], df2['y']):
  G.add_edge(x, x2, weight=w)

plt.figure(figsize=(15,15)) 

pos = nx.get_node_attributes(G, 'pos')
weights = nx.get_edge_attributes(G, 'weight') 
nx.draw(G, pos=pos, node_size=40, with_labels=True, fontsize=9)
nx.draw_networkx_edge_labels(G, pos=pos, edge_labels=weights)

plt.show()

I ran this a couple times before and it seemed to work, now however after re-opening jupyter notebook and running it again, it will not work.我之前运行过几次,它似乎有效,但是现在重新打开 jupyter notebook 并再次运行后,它不起作用。 I mainly have two major issues.我主要有两个主要问题。

  • If I try just running the line nx.draw(G, pos=pos, node_size=40, with_labels=True, fontsize=9) , my graph will show up, but no labels will be shown, even with with_labels set to true.如果我尝试只运行nx.draw(G, pos=pos, node_size=40, with_labels=True, fontsize=9) ,我的图形将显示,但不会显示任何标签,即使 with_labels 设置为 true。
  • Secondly this line nx.draw_networkx_edge_labels(G, pos=pos, edge_labels=weights) is now showing me the error can't multiply sequence by non-int of type 'float'其次,这一行nx.draw_networkx_edge_labels(G, pos=pos, edge_labels=weights)现在向我展示了错误不能将序列乘以非 int 类型的“float”

I have been looking at this for a couple of hours and I can't seem to fix it, any thoughts?我已经看了几个小时了,我似乎无法解决它,有什么想法吗?


Edit: I can get the labels to show up if a exclude pos=pos from nx.draw, but it will not work if I include it编辑:如果从 nx.draw 中排除 pos=pos,我可以让标签显示出来,但如果我包含它,它将不起作用

The problem is that you are not specifying any pos attribute for the node Winterfell , and then when you try to access it in draw_networkx_edge_labels it doesn't find it.问题是您没有为节点Winterfell指定任何pos属性,然后当您尝试在draw_networkx_edge_labels访问它时,它找不到它。

If you try giving it a position attribute, say:如果您尝试给它一个位置属性,请说:

      location    x    y
0      TheWall  145  570
1   Winterfell  142  520
2  WhiteHarbor  140  480

Then the attributes of all nodes can be correctly accessed and the network is corectly drawn:然后可以正确访问所有节点的属性并正确绘制网络:

plt.figure()
G=nx.Graph()

df1 = df1.reset_index(drop=True)
df2 = df2.reset_index(drop=True)

for i, x in enumerate(df1['location']):
    G.add_node(x, pos=(df1.loc[i,'x'], df1.loc[i,'y']))

for x, x2, w in zip(df2['location'], df2['x'], df2['y']):
    G.add_edge(x, x2, weight=w)

plt.figure(figsize=(15,15)) 

pos = nx.get_node_attributes(G, 'pos')
weights = nx.get_edge_attributes(G, 'weight') 
nx.draw(G, pos=pos, node_size=40, with_labels=True, fontsize=9)
nx.draw_networkx_edge_labels(G, pos=pos, edge_labels=weights)

plt.show()

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM