【问题标题】:interactive graph with matplotlib and networkx带有 matplotlib 和 networkx 的交互式图形
【发布时间】:2014-03-16 11:44:44
【问题描述】:

我的问题如下:

我想用 networkx 和 matplotlib 绘制图形。在显示图表时,我的程序应该处理一些其他的东西。所以我想出了matplotlib.pyplot.ion()函数来允许交互模式。

我的代码:

def print_graph(graph):
    """ prints the graph"""

    # stores the nodes and their name attributes in a dictionary
    nodes_names = nx.get_node_attributes(graph, "name")
    plt.ion()
    pos = nx.spring_layout(graph)

    # draw without labels, cuz it would label them with their adress, since we
    nx.draw(graph, pos, with_labels=False)

    # draw the label with the nodes_names containing the name attribute
    labels = nx.draw_networkx_labels(graph, pos, nodes_names)
    plt.show()

def setup_sending(graph, iteration):
"""method which handles sending messages in the network,
only call it once for sending sweet packages in a network"""
    print_graph(graph)

    ###some code doing calculations....

    raw_input('Press enter to continue')    

运行它并没有真正起作用。嗯,它会做所有的计算,甚至会打开一个图形窗口。但是这个应该显示图表的窗口正在处理一些东西并且保持白色。当然,按 Enter 后我会退出程序。

那么有人知道如何让代码显示图表吗?


编辑

 def setup_graph(laplacian):
""" this fucntion creates a graph object with the nodes and its edges
already correct initialized"""
    # this block adds the nodes to the graph and creates two dict
    # in order to label the graph correctly
    size = len(laplacian[0, :])
    my_graph = nx.Graph()
    for i in range(size):
        my_graph.add_node(Node(), name=str(i + 1))
    print(my_graph.nodes())
    # stores the nodes and their name attributes in a dictionary
    nodes_names = nx.get_node_attributes(my_graph, "name")
    # switches key and values--> thus names_nodes
    names_nodes = dict(zip(nodes_names.values(), nodes_names.keys()))

    # this block adds the edges between the nodes
    for i in range(0, size):
        for j in range(i + 1, size):
            if laplacian[i, j] == -1:
                node_1 = names_nodes[str(i + 1)]
                node_2 = names_nodes[str(j + 1)]
                my_graph.add_edge(node_1, node_2)

    return my_graph

Node() 只是一个包含某些列表的对象

【问题讨论】:

    标签: python matplotlib networkx


    【解决方案1】:

    这对我有用:

    import networkx as nx
    import matplotlib.pyplot as plt
    
    def print_graph(graph):
        """ prints the graph"""
    
        # stores the nodes and their name attributes in a dictionary
        nodes_names = nx.get_node_attributes(graph, "name")
        plt.ion()
        pos = nx.spring_layout(graph)
    
        # draw without labels, cuz it would label them with their adress, since we
        nx.draw(graph, pos, with_labels=False)
    
        # draw the label with the nodes_names containing the name attribute
        labels = nx.draw_networkx_labels(graph, pos, nodes_names)
        plt.show()
    
    def setup_sending(graph):
        print_graph(graph)
    
        ###some code doing calculations....
    
        raw_input('Press enter to continue')
    
    def main():
        G=nx.dodecahedral_graph()
        setup_sending(G)
    
    if __name__ == '__main__':
        main()
    

    所以,我认为您的问题可能是图表的大小。它有多大?它有多少个节点/边?

    【讨论】:

    • 它有 6 个节点。所以不会太大。
    • 我不确定,但我认为添加边的代码部分不正确。您一直在尝试按名称添加边,而不是按对象。因此,假设有两个节点:(a, name="foo") 和 (b, name="bar")。如果您在“foo”和“bar”之间添加一条边,那么您连接的不是“a”和“b”,而是另外两个名为“foo”和“bar”的新节点。我认为最好的办法是在 Node() 对象中添加 getter 方法。为了确保这是真正的问题,如果您在 print_graph(graph) 方法中打印图表会发生什么?我的意思是,试着把这样的东西:print str(graph.edges())
    • 比较print(my_graph.nodes())print(my_graph.edges())的输出,似乎节点对象是正确的。但是我不是 100% 确定,因为我在 python 方面没有太多经验。但据我了解,赋值 node_1 = names_nodes[str(i + 1)] 从字典中检索节点对象,只是让 node_1 指向同一个节点对象。
    • 所以,print str(graph.edges()) in print_graph(graph) 输出正确的图形,对吧?如果是,请在此处粘贴您的输出。
    • [<__main__.node>,<__main__.node>,<__main__.node>,<__main__.node>,<__main__.node>,<__main__.node>]
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-26
    • 2017-09-03
    • 1970-01-01
    • 2015-12-19
    • 2011-10-05
    • 2012-07-30
    相关资源
    最近更新 更多