-1

I'm using the following plot() function which I stumbled upon after experiments. previous question

def plot(self):
    plt.figure()
    pos = nx.graphviz_layout(self.g, prog='dot')
    nx.draw(self.g, pos, node_size=660, node_color='#aaaaff' )

The graph looks OK (it is hierarchal, with 3 layers) if the internally some edge connections use floating point numbers like the following example Look at 2.0, 3.0,... :

{1: {21: {}},
 2: {22: {}},
 3: {},
 21: {2.0: {}, 52: {}},
 22: {3.0: {}},
 52: {22.0: {}}}

enter image description here

But if the internals are pure integers like the example below everything goes WRONG :

{1: {21: {}},
 2: {22: {}},
 3: {},
21: {2: {}, 52: {}},
22: {3: {}},
52: {22: {}}}

enter image description here

It is the same for bigger graphs too. This was just one example.

Any idea why this may happen ?? and solution ...

Community
  • 1
  • 1
sten
  • 7,028
  • 9
  • 41
  • 63

1 Answers1

1

If NetworkX.draw internally produces a dictionary of edges, it's quite possible that the first example produces edges in a different order: floats and ints do not hash identically. I'm hardly an expert on this package, but I don't see anything in the NextworkX documentation that guarantees, or even hints, that it will print the graph in some form respecting the first-listed node as a visual root.

In short, you get the graph in whatever form the draw routine's internals find convenient.

I assume you can make the desired changes yourself; if not, please provide a fully-functional Minimal, complete, verifiable example. That promotes more detailed responses.


I did a little more reading. You can specify positions; if you default them (as you did in the posted example), then the layout method takes over, and you get whatever it finds convenient.

Community
  • 1
  • 1
Prune
  • 76,765
  • 14
  • 60
  • 81