Python 具有matplotlib和networkx的交互式图形

Python 具有matplotlib和networkx的交互式图形,python,matplotlib,networkx,Python,Matplotlib,Networkx,我的问题如下: 我想用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_no

我的问题如下:

我想用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')    
运行此功能实际上不起作用。Wll,它完成所有计算,甚至打开一个图形窗口。但是这个窗口应该显示图形正在处理某些东西,并且保持白色。按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()
只是一个包含特定列表的对象

这对我很有用:

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\u graph(graph)
方法中打印图形,会发生什么情况?我的意思是,尝试这样做:
print str(graph.edges())
比较
print(my\u graph.nodes())
print(my\u graph.edges())
似乎节点对象是正确的。但是我不是100%肯定,因为我在python方面没有太多经验。但据我所知,赋值
node\u 1=names\u nodes[str(I+1)]
从字典中检索node对象,并使node\u 1指向同一个node-object。因此,
print str(graph.edges())
print\u graph(graph)
中输出正确的图形,对吗?如果是,请将您的输出粘贴到此处。[,,,]