I am reading a JSON file and dynamically creating a graph using pygraphviz, using a simple loop:
hostdata = []
nodes = []
edges = {}
current_host = ""
trial = pgv.AGraph(strict=False, overlap=False)
for filename in os.listdir(options.directory):
with open(options.directory + "/" + filename, "r") as myfile:
hostdata = Truth(myfile.read().replace('\n', ''))
nodes.append(hostdata.host["something"])
current_something = hostdata.host["something"]
for key, value in hostdata.peer.iteritems():
nodes.append(key)
edges[current_something] = key
trial.add_edge(current_host, key)
The graph is complicated, but I really would prefer if the edges don't cross the nodes. I tried, when I set the strict and overlap, but I still have lines crossing over nodes.
This seems like something people would run into a lot, but I can't find anything on it. I am probably doing something completely wrong, or using the wrong search term. Any help appreciated.
This is happening because of the splines attribute of graphviz.
By default, the attribute is unset. How this is interpreted depends on the layout. For dot, the default is to draw edges as splines. For all other layouts, the default is to draw edges as line segments. Note that for these latter layouts, if splines="true", this requires non-overlapping nodes (cf. overlap). If fdp is used for layout and splines="compound", then the edges are drawn to avoid clusters as well as nodes.
Providing it as a named argument should solve the issue:
trial = pgv.AGraph(strict=False, overlap=False, splines='true')
#or
trial = pgv.AGraph(strict=False, overlap=False, splines='spline')