26
26
27
27
from plotnine import ggplot
28
28
from plotnine ._mpl .gridspec import p9GridSpec
29
+ from plotnine .iapi import outside_legend
29
30
from plotnine .typing import Side
30
31
31
32
# Note
@@ -79,7 +80,7 @@ class _side_spaces(ABC):
79
80
"""
80
81
81
82
def __post_init__ (self ):
82
- self .side = cast ("Side" , self .__class__ .__name__ [:- 7 ])
83
+ self .side : Side = cast ("Side" , self .__class__ .__name__ [:- 7 ])
83
84
"""
84
85
Side of the panel(s) that this class applies to
85
86
"""
@@ -136,17 +137,21 @@ def _legend_size(self) -> tuple[float, float]:
136
137
values e.g. 0.2, instead of just left, right, top, bottom &
137
138
center.
138
139
"""
139
- return (0 , 0 )
140
+ if not self .has_legend :
141
+ return (0 , 0 )
142
+
143
+ ol : outside_legend = getattr (self .items .legends , self .side )
144
+ return self .items .calc .size (ol .box )
140
145
141
146
@cached_property
142
- def _legend_width (self ) -> float :
147
+ def legend_width (self ) -> float :
143
148
"""
144
149
Return width of legend in figure coordinates
145
150
"""
146
151
return self ._legend_size [0 ]
147
152
148
153
@cached_property
149
- def _legend_height (self ) -> float :
154
+ def legend_height (self ) -> float :
150
155
"""
151
156
Return height of legend in figure coordinates
152
157
"""
@@ -220,6 +225,17 @@ def has_tag(self) -> bool:
220
225
"plot_tag_position"
221
226
)
222
227
228
+ @property
229
+ def has_legend (self ) -> bool :
230
+ """
231
+ Return True if the space/margin to this side of the panel has a legend
232
+
233
+ If it does, then it will be included in the layout
234
+ """
235
+ if not self .items .legends :
236
+ return False
237
+ return hasattr (self .items .legends , self .side )
238
+
223
239
224
240
@dataclass
225
241
class left_spaces (_side_spaces ):
@@ -257,7 +273,7 @@ def _calculate(self):
257
273
self .plot_tag_margin_right = m .r
258
274
259
275
if items .legends and items .legends .left :
260
- self .legend = self ._legend_width
276
+ self .legend = self .legend_width
261
277
self .legend_box_spacing = theme .getp ("legend_box_spacing" )
262
278
263
279
if items .axis_title_y :
@@ -283,13 +299,6 @@ def _calculate(self):
283
299
if adjustment > 0 :
284
300
self .plot_margin += adjustment
285
301
286
- @cached_property
287
- def _legend_size (self ) -> tuple [float , float ]:
288
- if not (self .items .legends and self .items .legends .left ):
289
- return (0 , 0 )
290
-
291
- return self .items .calc .size (self .items .legends .left .box )
292
-
293
302
@property
294
303
def offset (self ) -> float :
295
304
"""
@@ -370,7 +379,7 @@ def _calculate(self):
370
379
self .plot_tag_margin_left = m .l
371
380
372
381
if items .legends and items .legends .right :
373
- self .legend = self ._legend_width
382
+ self .legend = self .legend_width
374
383
self .legend_box_spacing = theme .getp ("legend_box_spacing" )
375
384
376
385
self .strip_text_y_width_right = items .strip_text_y_width ("right" )
@@ -383,13 +392,6 @@ def _calculate(self):
383
392
if adjustment > 0 :
384
393
self .plot_margin += adjustment
385
394
386
- @cached_property
387
- def _legend_size (self ) -> tuple [float , float ]:
388
- if not (self .items .legends and self .items .legends .right ):
389
- return (0 , 0 )
390
-
391
- return self .items .calc .size (self .items .legends .right .box )
392
-
393
395
@property
394
396
def offset (self ):
395
397
"""
@@ -490,7 +492,7 @@ def _calculate(self):
490
492
self .plot_subtitle_margin_bottom = m .b * F
491
493
492
494
if items .legends and items .legends .top :
493
- self .legend = self ._legend_height
495
+ self .legend = self .legend_height
494
496
self .legend_box_spacing = theme .getp ("legend_box_spacing" ) * F
495
497
496
498
self .strip_text_x_height_top = items .strip_text_x_height ("top" )
@@ -503,13 +505,6 @@ def _calculate(self):
503
505
if adjustment > 0 :
504
506
self .plot_margin += adjustment
505
507
506
- @cached_property
507
- def _legend_size (self ) -> tuple [float , float ]:
508
- if not (self .items .legends and self .items .legends .top ):
509
- return (0 , 0 )
510
-
511
- return self .items .calc .size (self .items .legends .top .box )
512
-
513
508
@property
514
509
def offset (self ) -> float :
515
510
"""
@@ -610,7 +605,7 @@ def _calculate(self):
610
605
self .plot_caption_margin_top = m .t * F
611
606
612
607
if items .legends and items .legends .bottom :
613
- self .legend = self ._legend_height
608
+ self .legend = self .legend_height
614
609
self .legend_box_spacing = theme .getp ("legend_box_spacing" ) * F
615
610
616
611
if items .axis_title_x :
@@ -635,13 +630,6 @@ def _calculate(self):
635
630
if adjustment > 0 :
636
631
self .plot_margin += adjustment
637
632
638
- @cached_property
639
- def _legend_size (self ) -> tuple [float , float ]:
640
- if not (self .items .legends and self .items .legends .bottom ):
641
- return (0 , 0 )
642
-
643
- return self .items .calc .size (self .items .legends .bottom .box )
644
-
645
633
@property
646
634
def offset (self ) -> float :
647
635
"""
0 commit comments