Skip to content

Commit 1eacc47

Browse files
committed
Adjust node as nodes get closer together in trees
1 parent 78ad745 commit 1eacc47

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

mathicsscript/format.py

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -218,14 +218,31 @@ def _hierarchy_pos(
218218
)
219219
# pos = {node:(leaf_vs_root_factor*x1+(1-leaf_vs_root_factor)*x2, y1) for ((x1,y1), (x2,y2)) in (leafpos[node], rootpos[node]) for node in rootpos}
220220
xmax = max(x for x, y in pos.values())
221+
y_list = {}
221222
for node in pos:
222-
pos[node] = (pos[node][0] * width / xmax, pos[node][1])
223-
return pos
223+
x, y = pos[node] = (pos[node][0] * width / xmax, pos[node][1])
224+
y_list[y] = y_list.get(y, set([]))
225+
y_list[y].add(x)
226+
227+
min_sep = xmax
228+
for y in y_list.keys():
229+
x_list = sorted(y_list[y])
230+
n = len(x_list) - 1
231+
if n <= 0:
232+
continue
233+
min_sep = min([x_list[i + 1] - x_list[i] for i in range(n)] + [min_sep])
234+
return pos, min_sep
235+
236+
237+
node_size = 300 # this is networkx's default size
224238

225239

226240
def tree_layout(G):
241+
global node_size
227242
root = G.root if hasattr(G, "root") else None
228-
return hierarchy_pos(G, root=root)
243+
pos, min_sep = hierarchy_pos(G, root=root)
244+
node_size = min_sep * 2000
245+
return pos
229246

230247

231248
NETWORKX_LAYOUTS = {
@@ -247,6 +264,9 @@ def format_graph(G):
247264
# FIXME handle graphviz as well
248265
import matplotlib.pyplot as plt
249266

267+
global node_size
268+
node_size = 300 # This is networkx's default
269+
250270
graph_layout = G.graph_layout if hasattr(G, "graph_layout") else None
251271
vertex_labeling = G.vertex_labeling if hasattr(G, "vertex_labeling") else False
252272
if vertex_labeling:
@@ -264,8 +284,8 @@ def format_graph(G):
264284
layout_fn = None
265285

266286
if layout_fn:
267-
nx.draw(G, pos=layout_fn(G), with_labels=vertex_labeling)
287+
nx.draw(G, pos=layout_fn(G), with_labels=vertex_labeling, node_size=node_size)
268288
else:
269-
nx.draw_shell(G, with_labels=vertex_labeling)
289+
nx.draw_shell(G, with_labels=vertex_labeling, node_size=node_size)
270290
plt.show()
271291
return None

0 commit comments

Comments
 (0)