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