Node Coloring and Updating Graph Plot

1,020 views
Skip to first unread message

Srinivas

unread,
Sep 26, 2009, 6:34:19 PM9/26/09
to networkx-discuss
Hi,
I am trying to do this in a large graph ---

1. Initially, all nodes in the graph have white color and connected by
black edges. It is displayed
by nx.draw(G, pos, node_color, edge_color ......) .
2. As my computation continues, the program computes and returns
certain nodes Nlist. I want only the nodes in Nlist to be colored
(and corresponding edges) in the displayed graph. This is needed until
the for loop ends.

I thought nx.draw and plt.show(), plt.hold(True) would keep the
current graph displayed and would "update" this plot as when it sees a
new nx.draw and plt.show(). But, that is not happening. I am forced to
"kill" the displayed figure and then the computation continues.

After reading through the matplotlib manual (page 199), I came to know
that such blocking calls are not supported.

I am not sure how to accomplish this. Any ideas? --- Briefly, I am
trying to build a hierarchy of nodes (starting from the sink down to
all individual nodes). As and when lower-level nodes are computed, I
need them to be shown (updated) nicely (colored) in the graph and
connected by a nice distinct color preferably the nodes and edges
shaded from dark to light....(like Red_r).

Perhaps, I can collect all information and then build the hierarchical
graph with appropriately shaded nodes/edges from the sink to the leaf
nodes --- giving some sleep time between each level of tree
construction...


Thanks,
Sri.

Dan Schult

unread,
Sep 27, 2009, 11:42:33 PM9/27/09
to networkx...@googlegroups.com
It sounds like you want interactive plotting but are using the batch
interface plt.show()
If you turn interactive plotting on with plt.ion() (can check with
plt.is_interactive())
then you should see the plot updating as you draw new colors. Then
use plt.savefig()
to save the picture as a file.

If the issue is more involved than my answer can fix and another
answer doesn't show up soon on this list you should try the
matplotlib user group as it sounds like an interactive/batch issue
with matplotlib.
Dan

Srinivas

unread,
Oct 1, 2009, 1:45:56 PM10/1/09
to networkx-discuss
Dear Dan,
Thanks for the suggestion. I am able to plot interactively.

=====================================
plt.ion()

elist = edge_list

# draw the basic, initial graph
nx.draw(G,pos, node_color='w', edge_color='k',
width=0.5,with_labels=True,alpha=0.8)

time.sleep(1)


# Now, begin updating by coloring nodes and edges

for nlist in MyNodeList:
# update: color each level nodes
nx.draw_networkx_nodes(G,pos,nodelist=nlist,
node_color='b',node_size=200,alpha=0.8)
time.sleep(1)

if i < len(elist):
nx.draw_networkx_edges(G,pos, edgelist=elist
[i],edge_color='r',
width=2,with_labels=True,alpha=0.8)


plt.plot() #-----> used this display the updated node
colors on top of previous graph plot.
time.sleep(1)

plt.show() #-- > used this to display the final graph and retain
in until I kill the figure window.


=====================================

Thanks again,
Sri.
Reply all
Reply to author
Forward
0 new messages