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,14 +318,16 @@ 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.
316- plot_widths = np .array (self .plot_widths )
317- panel_widths = np .array (self .panel_widths )
318- non_panel_space = plot_widths - panel_widths
319- new_plot_widths = panel_widths .mean () + non_panel_space
320- width_ratios = new_plot_widths / new_plot_widths .min ()
321+ # The new width of each panel is the total panel area scaled
322+ # by the factor given in plot_layout.
323+ # The new width of the plot includes the space not taken up
324+ # by the panels.
325+ factors = np .array (self .cmp ._plot_layout .widths )
326+ _totals = np .ones (len (self .panel_widths )) * sum (self .panel_widths )
327+ scaled_panel_widths = _totals * factors
328+ non_panel_space = np .array (self .plot_widths ) - self .panel_widths
329+ new_plot_widths = scaled_panel_widths + non_panel_space
330+ width_ratios = new_plot_widths / new_plot_widths .max ()
321331 self .gridspec .set_width_ratios (width_ratios )
322332 self .resize_sub_compositions ()
323333
@@ -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.
477- plot_heights = np .array (self .plot_heights )
478- panel_heights = np .array (self .panel_heights )
479- non_panel_space = plot_heights - panel_heights
480- new_plot_heights = panel_heights .mean () + non_panel_space
485+ # The new height of each panel is the total panel area scaled
486+ # by the factor given in plot_layout.
487+ # The new height of the plot includes the space not taken up
488+ # by the panels.
489+ factors = np .array (self .cmp ._plot_layout .heights )
490+ _totals = np .ones (len (self .panel_heights )) * sum (self .panel_heights )
491+ scaled_panel_heights = _totals * factors
492+ non_panel_space = np .array (self .plot_heights ) - self .panel_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