Re: [networkx-discuss] newbie req for info...

996 views
Skip to first unread message

Aric Hagberg

unread,
Oct 14, 2012, 12:37:50 PM10/14/12
to networkx...@googlegroups.com
On Sun, Oct 14, 2012 at 9:07 AM, alexxx <alexxx...@gmail.com> wrote:
> hi everybody,
> I discovered by chance networkX,
> and it seems very interesting: in a program I wrote recently I dealt with
> graph visualization by manually creating a dot file, and directly calling
> graphviz on it...
>
> One thing I always dreamed to do - but was unable until now:
> I'd like to visualize my graph in a window, and be able to catch in my
> program if the user clicks on a node.
> Can you tell be if networkX is able to draw clickable graphs?

Short answer: no.

Longer answer: NetworkX uses Matplotlib to do some basic graph
drawing. It is possible to handle events with Matplotlib like
clicking on a node or edge. But that would require writing some
Python code. Take a look at
http://matplotlib.org/users/event_handling.html for the capabilities
of Matplotlib.

Aric

alexxx

unread,
Oct 15, 2012, 7:35:41 AM10/15/12
to networkx-discuss
Aric, thank you for your help.
I'm not hostile at all to writing a bit of code, but from what I saw I
wonder if it is at all possible: I guess I could work with the pyplot
window that opens and displays the graph, using the (x,y) coordinates
at mouse-click, but I would need some kind of imagemap to know which
node has been visualized at that coordinates!

alessandro

On Oct 14, 6:38 pm, Aric Hagberg <aric.hagb...@gmail.com> wrote:
> Python code.  Take a look athttp://matplotlib.org/users/event_handling.htmlfor the capabilities
> of Matplotlib.
>
> Aric

Dan Schult

unread,
Oct 15, 2012, 10:26:20 AM10/15/12
to networkx...@googlegroups.com
Basically, once you get to pyplot you can do all those things you want interactively.
As you say though, you need a way to map back and forth between coordinate systems.

NetworkX provides positions for the nodes through its relatively simple layout functions.
Those positions are then used by matplotlib to draw the graph. Upon mouse-click
you'll need to get the (x,y) coords and translate them into the axes coordinates
to find the nearest node. There may be matplotlib tools/examples that do this
kind of thing already. NetworkX plots are just matplotlib's scatter plot.
Take a look at:
http://www.scipy.org/Cookbook/Matplotlib/Interactive_Plotting

Dan
> --
> You received this message because you are subscribed to the Google Groups "networkx-discuss" group.
> To post to this group, send email to networkx...@googlegroups.com.
> To unsubscribe from this group, send email to networkx-discu...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/networkx-discuss?hl=en.
>

alexxx

unread,
Oct 16, 2012, 8:39:31 AM10/16/12
to networkx-discuss
Thank you Dan!
the link you provided seems interesting and I tested ok
(provided that you use it this way:
x = range(10)
y = range(10)
annotes = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
fig = plt.figure()
ax = fig.add_subplot(111)
ax.scatter(x,y) # Generate the Scatter Plot.
af = AnnoteFinder(x,y, annotes)
fig.canvas.mpl_connect('button_press_event', af)
plt.show()
)

my remaining problem is that I dont know how to recover the (x,y)
coordinates from networkx (the annotes array yes, it's simply
G.nodes())
You say that NetworkX provides positions for the nodes through its
relatively simple layout functions - but I didnt find them...

alessandro
> >> Python code.  Take a look athttp://matplotlib.org/users/event_handling.htmlforthe capabilities

Dan Schult

unread,
Oct 16, 2012, 9:14:57 AM10/16/12
to networkx...@googlegroups.com
The drawing docs are here:
http://networkx.lanl.gov/reference/drawing.html

At the bottom are the layout routines. Many of the
more automated drawing routines use spring_layout
if no positions are specified in the call. Since you
need the positions, you'll have to call the layouts
first:

pos=nx.spring_layout(G)
nx.draw(G,pos)

Dan

alexxx

unread,
Oct 17, 2012, 4:24:30 AM10/17/12
to networkx-discuss
great, I was able to make it work!
Here is the code I used, making use of the class listed at
http://www.google.com/url?sa=D&q=http://www.scipy.org/Cookbook/Matplotlib/Interactive_Plotting&usg=AFQjCNFGoCVyLoGxZgF5ADT0uu8dkHvYpw

pos=nx.spring_layout(G) # the layout gives us the nodes position
x,y,annotes=[],[],[]
for key in pos:
d=pos[key]
annotes.append(key)
x.append(d[0])
y.append(d[1])

nx.draw(G,pos,node_size=sizelist,node_color=ncolors,edge_color=ecolors,font_size=8)

af = AnnoteFinder(x,y, annotes)
fig.canvas.mpl_connect('button_press_event', af)

thank you!

alessandro
Reply all
Reply to author
Forward
0 new messages