1
1
from __future__ import annotations
2
2
3
3
import abc
4
- from dataclasses import dataclass
4
+ from dataclasses import dataclass , field
5
5
from functools import cached_property
6
6
from typing import TYPE_CHECKING
7
7
@@ -72,7 +72,18 @@ class LayoutTree:
72
72
Each composition is a tree or subtree
73
73
"""
74
74
75
- gridspec : p9GridSpec
75
+ cmp : Compose
76
+ """
77
+ Composition that this tree represents
78
+ """
79
+
80
+ nodes : list [LayoutSpaces | LayoutTree ]
81
+ """
82
+ The spaces or tree of spaces in the composition that the tree
83
+ represents.
84
+ """
85
+
86
+ gridspec : p9GridSpec = field (init = False )
76
87
"""
77
88
Gridspec of the composition
78
89
@@ -86,11 +97,8 @@ class LayoutTree:
86
97
LayoutSpaces.
87
98
"""
88
99
89
- nodes : list [LayoutSpaces | LayoutTree ]
90
- """
91
- The spaces or tree of spaces in the composition that the tree
92
- represents.
93
- """
100
+ def __post_init__ (self ):
101
+ self .gridspec = self .cmp .gridspec
94
102
95
103
@staticmethod
96
104
def create (
@@ -122,9 +130,9 @@ def create(
122
130
nodes .append (LayoutTree .create (item , lookup_spaces ))
123
131
124
132
if isinstance (cmp , Beside ):
125
- return ColumnsTree (cmp . gridspec , nodes )
133
+ return ColumnsTree (cmp , nodes )
126
134
else :
127
- return RowsTree (cmp . gridspec , nodes )
135
+ return RowsTree (cmp , nodes )
128
136
129
137
@cached_property
130
138
def sub_compositions (self ) -> list [LayoutTree ]:
@@ -310,13 +318,15 @@ def resize(self):
310
318
"""
311
319
Resize the widths of gridspec so that panels have equal widths
312
320
"""
313
- # The new width of each panel is the average width of all
314
- # the panels plus all the space to the left and right
315
- # of the panels.
321
+ # The new width of each panel is the average width (scaled by
322
+ # the plot_layout widths) of all the panels plus all the space
323
+ # to the left and right of the panels.
324
+ widths = np .array (self .cmp ._plot_layout .widths )
316
325
plot_widths = np .array (self .plot_widths )
317
326
panel_widths = np .array (self .panel_widths )
318
327
non_panel_space = plot_widths - panel_widths
319
- new_plot_widths = panel_widths .mean () + non_panel_space
328
+ scaled_panel_widths = panel_widths .mean () * widths
329
+ new_plot_widths = scaled_panel_widths + non_panel_space
320
330
width_ratios = new_plot_widths / new_plot_widths .min ()
321
331
self .gridspec .set_width_ratios (width_ratios )
322
332
self .resize_sub_compositions ()
@@ -472,12 +482,15 @@ def resize(self):
472
482
473
483
This method resizes (recursively) the contained compositions
474
484
"""
475
- # The new height of each panel is the average width of all
476
- # the panels plus all the space above and below the panels.
485
+ # The new height of each panel is the average width (scaled by
486
+ # the plot_layout heights) of all the panels plus all the space
487
+ # above and below the panels.
488
+ heights = np .array (self .cmp ._plot_layout .heights )
477
489
plot_heights = np .array (self .plot_heights )
478
490
panel_heights = np .array (self .panel_heights )
479
491
non_panel_space = plot_heights - panel_heights
480
- new_plot_heights = panel_heights .mean () + non_panel_space
492
+ scaled_panel_heights = panel_heights .mean () * heights
493
+ new_plot_heights = scaled_panel_heights + non_panel_space
481
494
height_ratios = new_plot_heights / new_plot_heights .max ()
482
495
self .gridspec .set_height_ratios (height_ratios )
483
496
self .resize_sub_compositions ()
0 commit comments