如何解决networkx:显示边缘标签
H = nx.from_pandas_edgelist(links,source ='source',target ='target',edge_attr ='value')
这给了我边缘和高度的列表
edge_labels = nx.get_edge_attributes(Q,'value') nx.draw_networkx_edge_labels(Q,pos = nx.draw(Q),labels = edge_labels,font_size = 8) 这给出了一个错误
TypeError Traceback (most recent call last) <ipython-input-99-1783423b22ae> in <module>
----> 1 nx.draw_networkx_edge_labels(Q,pos=nx.draw(Q),labels=edge_labels,font_size=8)
~\Anaconda3\lib\site-packages\networkx\drawing\nx_pylab.py in draw_networkx_edge_labels(G,pos,edge_labels,label_pos,font_size,font_color,font_family,font_weight,alpha,bbox,ax,rotate,**kwds)
939 text_items = {}
940 for (n1,n2),label in labels.items():
--> 941 (x1,y1) = pos[n1]
942 (x2,y2) = pos[n2]
943 (x,y) = (x1 * label_pos + x2 * (1.0 - label_pos),TypeError: 'NoneType' object is not subscriptable
我不明白为什么会这样,但是这确实返回了一个没有节点名的图。 我想要的输出图是具有其名称和相应节点颜色的节点,并且在边缘显示“值”。 到目前为止,除边缘标签部分外,我一直很成功
解决方法
这是我为Networkx图加标签的标准模式-该示例是熊猫中数据库模式中FK引用的列表-输出是列表,因此您不必使用熊猫:
weightedGraphPairedLabelList = columnFKDataFrame[["TABLE_NAME","REFERENCED_TABLE_NAME"]].values.tolist()
thisGraph = nx.DiGraph()
thisGraph.add_edges_from(weightedGraphPairedLabelList)
print(random.choice(weightedGraphPairedLabelList))
Out: ['CreatingEntityAliases','CheckTableCEAliasTypes']
fig = plt.figure(figsize=(20,20))
thisAx = plt.subplot(111)
pos = nx.shell_layout(thisGraph) #this line is what is placing the nodes - in this case,using spring_layout
nx.draw(thisGraph,pos,node_size=50,edge_color='b',alpha=0.2,with_labels=True)
plt.title("Table Relationships")
plt.show()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。