timos
timos

Reputation: 2707

Spring graph layout with some predefined positions in networkx

I am using the networkx package and python3 to layout a simple graph. If I simply use the spring layout, the result is not as nice as it could be (if layouted by hand). So my idea was to fix the position of some nodes. Currently I use the following code:

%matplotlib inline

import matplotlib
import numpy as np
import matplotlib.pyplot as plt
import networkx as nx


G = nx.DiGraph()
G.add_path(['H0', 'S0', 'S1', 'S5', 'S6', 'S7', 'H3'], color='green')
G.add_path(['H5', 'S4', 'S3', 'S2', 'S1', 'S0', 'H1'], color='red')
G.add_path(['H2', 'S7', 'S6', 'S8', 'S3', 'S4', 'H4'], color='blue')

#explicitly set some positions to make the drawing nice
fixed_pos = {'S1':(-1,0),'S5':(0,0), 'S6':(1,0), 'S3':(0,1)}
fixed_nodes = fixed_pos.keys()
pos = nx.spring_layout(G, pos=fixed_pos, fixed = fixed_nodes, scale=3)

nx.draw_networkx(G,pos, font_size=10, font_family='sans-serif')
plt.show()

The result I get looks like this:

enter image description here

So all the nodes with a fixed position get crammed arround 0.5,0.5! Can anyone tell me why this is happening?

Upvotes: 1

Views: 1552

Answers (1)

Joel
Joel

Reputation: 23827

You either have something weird with one of your installations, or there's something a little different about the code you're running.

After copying and pasting your code exactly (python2.7), I get:enter image description here

So the nodes are where you specified.

Upvotes: 1

Related Questions