79650086

Date: 2025-06-02 17:35:16
Score: 1
Natty:
Report link

Yes it's possible.The reason your original code didn't behave like your DOT example is because NetworkX doesn't automatically use fixed positions unless you explicitly define them in Python. As @Sanek Zhitnik pointed out a graph doesn't have an orientation.

In your DOT format example, you had this:

dot
    digraph {
    layout="neato"
    node[ shape=rect ];

    a [ pos = "2,3!" ];
    b [ pos = "1,2!" ];
    c [ pos = "3,1!" ];
    d [ pos = "4,0!" ];

    c -> d ;
    b -> c ;
    b -> a ;
    a -> c ;
}

You have to do something like this:

import networkx as nx
import matplotlib.pyplot as plt

G = nx.DiGraph()
G.add_edges_from([('b','a'), ('b','c'), ('a','c'),('c','d')])

# Fixed positions (same layout every time)
pos = {
    'a': (2, 3),
    'b': (1, 2),
    'c': (3, 1),
    'd': (4, 0)
}


nx.draw(G, pos, with_labels=True, node_shape='s', node_color='lightblue', arrows=True)
plt.show()
Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • User mentioned (1): @Sanek
  • Low reputation (1):
Posted by: Rian