nx.draw and plt.Circle

741 views
Skip to first unread message

vjktmail

unread,
Dec 27, 2013, 3:46:02 PM12/27/13
to networkx...@googlegroups.com
I'd like to overlay a circle on a graph drawn by nx.draw(). E.g., a circle around a specific node.
I tried:

nx.draw(...)
plt.Circle(...)
plt.show()

But the circle does not appear.  Note I used one of the pos values that nx.draw(..) was given for the circle center.

Is this possible?  What am I doing wrong?

Daniel Schult

unread,
Dec 27, 2013, 4:22:05 PM12/27/13
to networkx...@googlegroups.com
Can you create a <10 line example that doesn't work?


--
You received this message because you are subscribed to the Google Groups "networkx-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to networkx-discu...@googlegroups.com.
To post to this group, send email to networkx...@googlegroups.com.
Visit this group at http://groups.google.com/group/networkx-discuss.
For more options, visit https://groups.google.com/groups/opt_out.

vjktmail

unread,
Dec 28, 2013, 8:56:14 AM12/28/13
to networkx...@googlegroups.com
Hi Daniel,
I forgot to mention I tried it in IPython.  Here is an example:

import networkx as nx
gsize = 4 #square grid size
gg=nx.generators.grid_2d_graph(gsize,gsize) #grid graph gg
posg = nx.layout.fruchterman_reingold_layout(gg)
nx.draw(gg,posg)
center = posg[(0,0)]
plt.Circle(center, radius=5)
plt.show()

vjktmail

unread,
Dec 28, 2013, 8:57:42 AM12/28/13
to networkx...@googlegroups.com
I meant to say I used IPython Notebook.

Aric Hagberg

unread,
Dec 28, 2013, 10:45:18 AM12/28/13
to networkx...@googlegroups.com
Try
import matplotlib.pyplot as plt
import networkx as nx
gsize = 4 #square grid size
gg=nx.generators.grid_2d_graph(gsize,gsize) #grid graph gg
posg = nx.layout.fruchterman_reingold_layout(gg)
nx.draw(gg,posg)
center = posg[(0,0)]
ax = plt.gca()
c = plt.Circle(center, radius=0.1)
ax.add_patch(c)
plt.axis('equal')
plt.show()

Daniel Schult

unread,
Dec 28, 2013, 10:47:15 AM12/28/13
to networkx...@googlegroups.com
I see two potential problems:
1) you need to add the circle to the axes.  See
2) your radius is much larger than the position values of the nodes in the network. You could try radius=.15 (but you'll still need to add the circle to the axes).
Dan

vjktmail

unread,
Dec 28, 2013, 6:32:17 PM12/28/13
to networkx...@googlegroups.com
Daniel, Aric,
thank you both!
I ran Aric's example successfully.  I didn't realize that I had to use the axes; I need to hit the pyplot documentation.
Thanks.
Reply all
Reply to author
Forward
0 new messages