Reputation: 1070
I have a tree which I plot with Graphviz, however the distances between the nodes is very large and not efficiently used. Can this be improved?
I tried playing with mindist
, splines
and overlap
, but nothing seems to work. Is this the best graphviz is able to do?
The data:
The code:
import json
import pygraphviz as pgv
# Read data from the "graph1.json" file
with open("graph2.json") as f:
edges_data = json.load(f)
# Create a new directed graph
graph = pgv.AGraph(strict=False, directed=True)
# Add nodes and edges to the graph based on the JSON data
for edge in edges_data:
graph.add_node(edge["ID"])
if edge["ParentID"]:
graph.add_edge(edge["ParentID"], edge["ID"])
graph_attrs = {
'dpi': 50,
'root': 10000185,
'mindist': 0.1
# 'splines': False, # false
# 'overlap':'scale' # false
}
graph.graph_attr.update(graph_attrs)
graph.draw("output_graph.svg", prog="circo", format="svg")
Upvotes: 0
Views: 134
Reputation: 6791
You have probably hit this circo bug:
Trying to find a circo work-around will probably not lead to success (unless you feel lucky)
Try the other layout engines, twopi, neato, fdp, and dot
Upvotes: 1
Reputation: 9418
Graphviz has the attribute len
:
Preferred edge length, in inches
But:
Note: neato, fdp only.
See also: How to specify the length of an edge in graphviz?.
So, you can add:
graph.edge_attr["len"] = 2 # Preferred edge length, in inches, works only with layout-engines neato or fdp
graph.draw("output_graph.png", prog="neato")
Setting the preferred edge length of 2 inches, the output of given 4973 nodes rendered by neato engine becomes an illegible, overlapping snowflake like in this picture:
Another attempt with 4 inches produces:
Note: the rendering took longer, also the file size increased from the 2-inches PNG's 2.2M to 15M.
The text is still not readable with 6 inches edge-length:
Now we have reached 22M for the PNG.
Upvotes: 1