获得有序的DiGraph边缘

时间:2019-02-21 14:46:27

标签: networkx

这是我要绘制的定向网络。 Plot of DiGraph Network with colored edges 边缘从颜色图中获取其特定颜色。色图链接到每个边缘温度。 因此:

边缘编号:13温度为:14°C→边缘颜色应为蓝色,但应为红色

边缘编号:14温度为:38°C→边缘颜色应为红色,但应为蓝色

问题是,我没有得到边缘或边缘颜色的统一顺序。我不知道边缘或拐角是否无序。

问题已在以下内容中描述: https://networkx.github.io/documentation/latest/reference/classes/ordered.html 我正在使用Python 3.7。 nx.DiGraph()或nx.OrderedDiGraph()无法获得正确的顺序。没有真正的答案,如何防止networkx混合边缘顺序。 有人可以帮我吗? :)

这是我的代码,随时可以编译:

error FABLE: Cannot resolve System.Enum.Parse

1 个答案:

答案 0 :(得分:0)

您的规格完全可以。在当前版本的networkx中,这似乎是一个错误。我会在他们的github上提出一个问题。

作为解决方法,您可以使用我的绘图工具分叉netgraph。 我强制使用字典来指定节点和边属性(如果您希望它们在节点/边之间有所不同)以防止出现此问题。

enter image description here

import netgraph # install with: pip install netgraph
from matplotlib import colors, cm

# --------------------------------------------------------------------------------
# your specifications

nodes_id = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21]
nodes_x_y = [(1, 1.0), (2, 1.0), (3, 1.0), (4, 1.0), (5, 1.0), (2, 1.5), (3, 1.5), (4, 1.5),
             (5, 1.5), (2, 2.0), (3, 2.0), (4, 2.0), (5, 2.0), (2, 2.5), (3, 2.5), (4, 2.5),
             (5, 2.5), (1, 3.0), (2, 3.0), (3, 3.0), (4, 3.0), (5, 3.0)]
# edges input         those should match together↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ those should match together
# edge labels
edges_id =      [0,     1,     2,    3,    4,    5,   6,    7,    8,    9,    10,   11,   12,   13,   14,    15,  16,   17,   18,   19,   20,   21,   22,    23]
# edge temperatures
edges_tp_forw = [35.0, 33.0, 33.0, 33.0, 24.0, 33.0, 33.0, 33.0, 24.0, 14.0, 38.0, 33.0, 24.0, 14.0, 38.0, 33.0, 40.0, 14.0, 38.0, 33.0, 40.0, 40.0, 38.0, 33.0]
#                     those should match together↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑ those should match together
edges_u = [1, 2, 3, 4, 1, 6, 7, 8, 5, 10, 7, 12, 9, 14, 11, 16, 13, 19, 15, 21, 18, 19, 20, 20]
edges_v = [0, 1, 2, 3, 5, 2, 3, 4, 9, 6, 11, 8, 13, 10, 15, 12, 18, 14, 20, 16, 17, 18, 19, 21]
edges_labels_id_tp = [pair for pair in zip(edges_id, edges_tp_forw)]

# --------------------------------------------------------------------------------
# conversion of your specifications to dictionaries

edges = list(zip(edges_u, edges_v))
node_positions = dict(zip(nodes_id, nodes_x_y))
node_labels = {node : str(node) for node in nodes_id}
color_map = cm.ScalarMappable(norm=colors.Normalize(vmin=min(edges_tp_forw),
                                                     vmax=max(edges_tp_forw)),
                               cmap=plt.get_cmap('coolwarm'))
edge_color = {edge : color_map.to_rgba(val) for edge, val in zip(edges, edges_tp_forw)}
edge_labels = {edge : str(pair) for edge, pair in zip(edges, edges_labels_id_tp)}

fig, ax = plt.subplots(1, 1, figsize=(10,5))
netgraph.draw(edges,
              node_positions       = node_positions,
              node_edge_width      = 0.,
              node_color           = 'lightgray',
              node_labels          = node_labels,
              node_label_font_size = 6,
              edge_color           = edge_color,
              edge_labels          = edge_labels,
              edge_label_font_size = 6,
              rotate               = False,
              draw_arrows          = True,
              ax                   = ax)
fig.tight_layout()
plt.show()