简体   繁体   English

如何在python networkx中显示图形节点的标签

[英]How can I display label for graph nodes in python networkx

I have following code to visualize networkx graph:我有以下代码来可视化 networkx 图:

node_positions = networkx.spring_layout(graph)
networkx.draw_networkx_nodes(graph, node_positions)
networkx.draw_networkx_edges(graph, node_positions, style='dashed')

I want to display node label on each node.我想在每个节点上显示节点标签。 graph.nodes() will give me list of all the nodes but how can I use them to label nodes on the graph? graph.nodes()会给我所有节点的列表,但我如何使用它们来标记图形上的节点?

I tried this, but it is not working:我试过这个,但它不起作用:

networkx.draw_networkx_labels(graph,node_positions,graph.nodes(data="false"),font_size=16)

EDIT:编辑:

I tried following and its working for now:我尝试了以下并且它现在可以工作:

for node,_ in graph.nodes(data="False"):
        labels[node] = node;
networkx.draw_networkx_labels(graph,node_positions,labels,font_size=16)

If there is still some elegant way to do it please let me know.如果还有一些优雅的方法可以做到这一点,请告诉我。 I feel creating new dict is overkill, redundant and should not be required.我觉得创建新的 dict 是矫枉过正,多余的,不应该是必需的。

在此处输入图片说明 Check nx.draw source code or drawing documentation .检查nx.draw 源代码绘图文档 I am not quite sure I got your question correctly because according to my understanding it is a fairly straight forward issue, simply you are allowed to add labels for a graph as follows:我不太确定我是否正确回答了您的问题,因为根据我的理解,这是一个相当直接的问题,您只需为图表添加标签,如下所示:

networkx.draw(graph, node_positions, with_labels = True)

You can also, use draw_spring directly without having to explicity assign node positions:您也可以直接使用draw_spring而不必显式分配节点位置:

networkx.draw_spring(graph, with_labels = True)

The figure shows the result of running the following code:下图显示了运行以下代码的结果:

import networkx as nx
G = nx.erdos_renyi_graph(10,0.4)
nx.draw(G, with_labels=True)
plt.show()

Your problem is that you sent the list of graph nodes in place of an optional argument where it expected a dict.您的问题是您发送了图形节点列表,而不是它期望 dict 的可选参数。 By having this optional argument, networkx allows you to have whatever label you want appear.通过使用这个可选参数,networkx 允许您显示您想要的任何标签。 In your case you want the obvious label of just the node's name.在您的情况下,您只需要节点名称的明显标签。 This is the default behavior.这是默认行为。 So simply就这么简单

networkx.draw_networkx_labels(graph,node_positions,font_size=16)

will suffice.就足够了。 Don't send the dict and it will use the node names to label them.不要发送字典,它将使用节点名称来标记它们。

An alternate option is to do exactly what abdallah suggested, and draw the entire graph in one networkx.draw command with the with_labels=True option.另一种选择是完全按照 abdallah 的建议进行操作,并使用with_labels=True选项在一个networkx.draw命令中绘制整个图形。


brief comment on format of your question对您的问题格式的简要评论

When I initially read your question, I assumed the draw_networkx_labels command was giving an output different from what you expected.当我最初阅读您的问题时,我认为draw_networkx_labels命令给出的输出与您的预期不同。

Now that I've tried your code, I see it actually gives an error.现在我已经尝试了你的代码,我发现它实际上给出了一个错误。 Telling us that and that the error is告诉我们错误是

AttributeError: 'list' object has no attribute 'items' AttributeError: 'list' 对象没有属性 'items'

would have made it much easier to figure out what was wrong, so for future reference if you get an error message, it contains information about what the problem is.会更容易找出问题所在,因此如果您收到错误消息,以供将来参考,它包含有关问题所在的信息。 If you post a question, post the error message as well.如果您发布问题,请同时发布错误消息。

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

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