Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am testing out how to create network graphs on "networkx"; my problem is that when I try plotting these graphs using "matplotlib", the nodes, edges, and labels appear jumbled. I want to have the labels attached to right node, and I want the edges to look like they are connecting the nodes.

code

import networkx as nx
try:
    import matplotlib.pyplot as plt
except:
    raise

g = nx.MultiGraph()

strList = ["rick james", "will smith", "steve miller", "rackem willie", "little tunechi", "ben franklin"]
strList2 = ["jules caesar", "atticus finch", "al capone", "abe lincoln", "walt white", "doc seuss"]
i = 0
while i < len(strList) :
    g.add_edge(strList[i], strList2[i])
    i = i + 1    

nx.draw_networkx_nodes(g,pos = nx.spring_layout(g), nodelist = g.nodes())
nx.draw_networkx_edges(g,pos = nx.spring_layout(g), edgelist = g.edges())
nx.draw_networkx_labels(g,pos=nx.spring_layout(g))
#plt.savefig("testImage.png")
plt.show()

image

[1] http://imgur.com/PIvEm3y

I want my connections to be like this:

rick james <--> jules caesar
will smith <--> atticus finch
steve miller <--> al capone
...etc

Any help/insight is greatly appreciated!

share|improve this question

2 Answers 2

up vote 1 down vote accepted

The spring layout is stochastic (random). One issue you're having comes from the fact that you are running this stochastic process a different time -- producing a different layout -- for the nodes, edges, and labels. Try this, where you compute the layout a single time:

pos = nx.spring_layout(g)
nx.draw_networkx_nodes(g, pos=pos, nodelist = g.nodes())
nx.draw_networkx_edges(g, pos=pos, edgelist = g.edges())
nx.draw_networkx_labels(g, pos=pos)

Or in the case that you don't need to style individual nodes/edges/labels:

nx.draw_spring(g)

I won't claim that this will give you a "good" layout because it doesn't (at least on my machine):

spring-layout

Maybe a networkx.draw_circular layout would be a better fit:

nx.draw_circular(g)

circular-layout

You can read about all the NetworkX layouts here, including Graphviz (as suggested by @ThomasHobohm).

share|improve this answer
    
thanks alot. I have a followup question. The code I used to create the last image (circular one) is this: LINE 1: pos = nx.circular_layout(g) LINE 2: nx.draw_networkx_labels(g, pos=pos) LINE 3: nx.draw_circular(g) LINE 4: plt.show() is this the best practice/ will this always guarantee my labels and nodes will be aligned? –  Rick James 16 hours ago
    
@RickJames: unless you are going to change the labels with nx.draw_networkx_labels, just use nx.draw_circular. But nx.draw_circular is not stochastic, so yes your nodes and labels will be aligned. –  mdml 16 hours ago
    
When I use nx.draw_circular(g) the labels are not included. Where would I indicate that I want the labels to be included? –  Rick James 16 hours ago
    
@RickJames: what version of NetworkX are you using? By default they should be included with the latest version (1.9). –  mdml 16 hours ago
    
hmm I just checked the version and it is 1.9 –  Rick James 16 hours ago

Matplotlib is very bad at drawing readable graphs. I suggest you use Graphviz, at it's supported by NetworkX out of the box, and it let's you toggle way more settings with the dot format.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.