WG-
WG-

Reputation: 1070

Graphviz unnecessarily long edges / distances between nodes

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? enter image description here

I tried playing with mindist, splines and overlap, but nothing seems to work. Is this the best graphviz is able to do?

The data:

https://pastebin.com/JUC7GhLV

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

Answers (2)

sroush
sroush

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

hc_dev
hc_dev

Reputation: 9418

Edge length attribute

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?.

Example with 2 inches

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:

snowflake with edges of 2 inches length

Example with 4 inches

Another attempt with 4 inches produces:

snowflake with edge-length 4 inches

Note: the rendering took longer, also the file size increased from the 2-inches PNG's 2.2M to 15M.

Example with 6 inches

The text is still not readable with 6 inches edge-length:

snowflake with edge-length 6 inches

Now we have reached 22M for the PNG.

Upvotes: 1

Related Questions