11from __future__ import annotations
22
33import abc
4- from dataclasses import dataclass
4+ from dataclasses import dataclass , field
55from functools import cached_property
66from typing import TYPE_CHECKING
77
@@ -72,7 +72,18 @@ class LayoutTree:
7272 Each composition is a tree or subtree
7373 """
7474
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 )
7687 """
7788 Gridspec of the composition
7889
@@ -86,11 +97,8 @@ class LayoutTree:
8697 LayoutSpaces.
8798 """
8899
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
94102
95103 @staticmethod
96104 def create (
@@ -122,9 +130,9 @@ def create(
122130 nodes .append (LayoutTree .create (item , lookup_spaces ))
123131
124132 if isinstance (cmp , Beside ):
125- return ColumnsTree (cmp . gridspec , nodes )
133+ return ColumnsTree (cmp , nodes )
126134 else :
127- return RowsTree (cmp . gridspec , nodes )
135+ return RowsTree (cmp , nodes )
128136
129137 @cached_property
130138 def sub_compositions (self ) -> list [LayoutTree ]:
@@ -310,13 +318,15 @@ def resize(self):
310318 """
311319 Resize the widths of gridspec so that panels have equal widths
312320 """
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 )
316325 plot_widths = np .array (self .plot_widths )
317326 panel_widths = np .array (self .panel_widths )
318327 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
320330 width_ratios = new_plot_widths / new_plot_widths .min ()
321331 self .gridspec .set_width_ratios (width_ratios )
322332 self .resize_sub_compositions ()
@@ -472,12 +482,15 @@ def resize(self):
472482
473483 This method resizes (recursively) the contained compositions
474484 """
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 )
477489 plot_heights = np .array (self .plot_heights )
478490 panel_heights = np .array (self .panel_heights )
479491 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
481494 height_ratios = new_plot_heights / new_plot_heights .max ()
482495 self .gridspec .set_height_ratios (height_ratios )
483496 self .resize_sub_compositions ()
0 commit comments