【问题标题】:python networkx- save disconnected graphs from single figure into separate filespython networkx-将断开连接的图形从单个图形保存到单独的文件中
【发布时间】:2015-11-13 21:02:18
【问题描述】:

我有以下程序可以生成我的图表并将其显示在单个图形上。

边2 = [(1, 2), (1, 3), (1, 4), (4, 5), (6, 7), (6,8)]

G = nx.DiGraph()

生成图形的函数如下:

    def create_graph(G,nodes,Sets):

G.add_edges_from(nodes)

#value assigned to each world
custom_labels={}
custom_node_sizes={}
node_colours=['y']

for i in range(0, len(Sets)):
    custom_labels[i+1] = Sets[i]
    custom_node_sizes[i+1] = 5000
    if i < len(Sets):
        node_colours.append('b')

 

nx.draw(G,labels=custom_labels,node_list = nodes,node_color=node_colours, node_size=custom_node_sizes.values())
#show with custom labels
plt.show()

对于上述函数,我正在传递边列表 (Edges2)。 该函数在一个图形上生成两个断开连接的图形。但是,我想分别保存这两个图表。

所以基本上,有没有办法将两个断开连接的图形保存到两个文件中?所以,我可以得到graph1.png和graph2.png。

【问题讨论】:

  • 你将什么作为输入'Sets'发送到函数?

标签: python python-2.7 networkx digraphs


【解决方案1】:

我没有完全得到你的函数的“集合”输入,或者你为什么add_edges_from(nodes) 你使用节点而不是边作为输入!。因此,为了回答您在 2 个单独的文件中绘制断开连接图的问题,我在没有 custom_labels 的情况下重现了该问题,因为它取决于“集”输入,并且我也将 Edges2 作为节点和集的输入发送。正如@joel 所建议的,我使用了weakly_connected_component_subgraphs 函数,然后循环该函数的输出,分别保存每个图形。所以最终原始图保存在 original_graph.png 中,子图分别保存在 graph1.pnggraph2.png 中。

def create_graph(G,nodes,Sets):
    G.add_edges_from(nodes)

    #value assigned to each world
    custom_labels={}
    custom_node_sizes={}
    node_colours=['y']

    for i in range(0, len(Sets)):
        custom_labels[i+1] = Sets[i]
        custom_node_sizes[i+1] = 5000
        if i < len(Sets):
            node_colours.append('b')
    nx.draw(G,node_list = nodes,node_color=node_colours, node_size=1000, with_labels = True)
    plt.savefig("original_graph.png")
    plt.show()
    G_comp = nx.weakly_connected_component_subgraphs(G)
    i =  1 
    for comp in G_comp:
        nx.draw(comp,node_color=node_colours,  node_size=1000, with_labels=True)
        #show with custom labels
        fig_name = "graph" + str(i) + ".png"
        plt.savefig(fig_name)
        plt.show()
        i += 1


Edges2 = [(1, 2), (1, 3), (1, 4), (4, 5), (6, 7), (6,8)]
G = nx.DiGraph()
create_graph(G,Edges2,Edges2)

原始图表

graph1 和 graph2

由 AKI 编辑: 我已经添加了我需要的标签(参见 cmets)。代码的最后一部分是:

    i =  1
    custom_number = 1;
    for comp in G_comp:
        dictfilt = lambda x, y: dict([ (i,x[i]) for i in x if i in set(y) ])
        wanted_keys = (range(custom_number,custom_number + len(comp)))
        newdict = dictfilt(custom_labels, wanted_keys)

        nx.draw(comp,node_color=node_colours,  node_size=1000, with_labels=True, labels = newdict)
        #show with custom labels
        fig_name = "graph" + str(i) + ".png"
        plt.savefig(fig_name)
        plt.show()
        custom_number += len(comp)
        i += 1

这个改进的版本从字典中提取必要的数据。 非常感谢@author的回答

【讨论】:

  • 集合列表可以如下: Sets2 = [[('or', 'r', 't')], [('not', 'r'), 'r'] , [('not', 's'), 'r'], ['r'], [('box', 's')],[('box', 'b')],[(' box', 'b')], [('box', 'c')]]
  • 事实上,如果我使用我的标签,它不会给我正确的图表,graph2 会给我一个错误。有没有办法让这些自定义标签出现在两个单独的图表上?我的标签在原始图上工作正常,但出现拆分图错误
  • 我仍然不明白 'Sets' 代表什么,但一般来说,如果你想显示标签,你必须创建一个 labels 字典,将每个节点映射到一个标签然后 nx.draw(G, labels = labels, with_labels=True)
  • 需要为每个节点显示集合。因此,与其放置 1,不如将其设置为 [('or', 'r', 't')],重要的是显示它而不是显示数字。当我执行您建议的操作时,我根本没有得到 graph2。我已经有 custom_labels={} 我可以以某种方式引用它,以便每个节点按给定顺序对应一个标签吗?
  • 我很难理解你是如何创建你的 custom_labels 的,例如为什么你 custom_label[i+1] 这样你没有给节点 0 一个标签!
【解决方案2】:

由于您的图表是有向,您需要提取图表的强连通分量

graphs = nx.strongly_connected_component_subgraphs(G)

更多详情可以查看here
还有一些其他有用的组件方法,你可以找到here

【讨论】:

  • connected_component_subgraphs 没有给出 连通分量,实际上也不是为有向图编写的。你的意思是strongly_connected_component_subgraphs但是我认为 OP 实际上想要弱连接组件:weakly_connected_component_subgraphs。如果您的回答给出了如何分别绘制组件的说明,这也会有所帮助。
  • 是的,你是对的,我的意思是强连接 comp,但我添加了错误的代码。谢谢 ;)
  • 今天晚些时候我会测试它,但是如果有人写了我需要的类似功能,请分享:)。
  • 所以对于输入 Edges2 = [(1, 2), (1, 3), (1, 4), (4, 5), (6, 7), (6,8) ],我正在使用以下内容:graphs = list(nx.strongly_connected_component_subgraphs(G))。我的结果是: [, , ... ] 我想了一些是错误的,因为我有 2 个组件,我得到了 8 个......
  • 另外,如何将它们分别保存为 .png 文件?
猜你喜欢
  • 2014-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-21
  • 2021-05-14
  • 2022-01-01
相关资源
最近更新 更多