本文介绍了networkx-根据边缘属性更改颜色/宽度-不一致的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设法正确生成了图形,但是在进行了更多测试后,发现以下两个不同代码行的结果不一致:

I managed to produce the graph correctly, but with some more testing noted inconsistent result for the following two different line of codes:

nx.draw_circular(h,edge_color=[h.edge[i][j]['color'] for (i,j) in h.edges_iter()], width=[h.edge[i][j]['width'] for (i,j) in h.edges_iter()])

nx.draw_circular(h,edge_color=list(nx.get_edge_attributes(h,'color').values()), width=list(nx.get_edge_attributes(h,'width').values()))

第一行产生一致的输出,而第二行则按边缘顺序产生错误的颜色/大小.

The first line results consistent output, while the second produce wrong color/size per the orders of edges.

但是,在我看来,以上两行都依赖于函数调用以按边缘顺序返回属性.为什么结果不同?

However, it looks to me the above two lines both rely on the function call to return the attributes per the order of edges. Why the different results?

使用h[][][]访问属性对我来说似乎有点笨拙;是否可以通过点约定来访问它,例如edge.color for edge in h.edges().

It looks a bit clumsy to me to access attributes with h[][][]; is it possible to access it by dot convention, e.g. edge.color for edge in h.edges().

还是我错过了什么?

推荐答案

传递到绘图函数的边的顺序很重要.如果不指定(使用edges关键字),则将获得G.edges()的默认顺序.显式给出如下参数是最安全的:

The order of the edges passed to the drawing functions are important. If you don't specify (using the edges keyword) you'll get the default order of G.edges(). It is safest to explicitly give the parameter like this:

import networkx as nx

G = nx.Graph()
G.add_edge(1,2,color='r',weight=2)
G.add_edge(2,3,color='b',weight=4)
G.add_edge(3,4,color='g',weight=6)

pos = nx.circular_layout(G)

edges = G.edges()
colors = [G[u][v]['color'] for u,v in edges]
weights = [G[u][v]['weight'] for u,v in edges]

nx.draw(G, pos, edges=edges, edge_color=colors, width=weights)

这将导致如下输出:

This results in an output like this:

这篇关于networkx-根据边缘属性更改颜色/宽度-不一致的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 14:32