Frage

My probelm is the following:

I want to draw a graph with networkx and matplotlib. While showing the graph my programm is supposed to process some other stuff. So I came up with the matplotlib.pyplot.ion()function to allow the interactive mode.

My code:

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')    

running this does not really work. Wll, it does all the calculations and even opens a figure-window. But this window which should show the graph is processing something and remains white. Of coures after pressing Enter I exit the programm.

So does anyone have an idea how to make the code show the graph?


EDIT

 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() is just a object containing certain lists

War es hilfreich?

Lösung

This works for me:

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()

so, I think your problem could be the size of your graph. How large is it? How many nodes/edges does it have?

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top