-
Notifications
You must be signed in to change notification settings - Fork 252
Expand file tree
/
Copy path_plot_side_space.py
More file actions
1176 lines (977 loc) · 36.7 KB
/
Copy path_plot_side_space.py
File metadata and controls
1176 lines (977 loc) · 36.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Routines to adjust subplot params so that subplots are
nicely fit in the figure. In doing so, only axis labels, tick labels, axes
titles and offsetboxes that are anchored to axes are currently considered.
Internally, this module assumes that the margins (left margin, etc.) which are
differences between `Axes.get_tightbbox` and `Axes.bbox` are independent of
Axes position. This may fail if `Axes.adjustable` is `datalim` as well as
such cases as when left or right margin are affected by xlabel.
"""
from __future__ import annotations
from dataclasses import replace
from functools import cached_property
from typing import TYPE_CHECKING
from plotnine.exceptions import PlotnineError
from plotnine.facets import facet_grid, facet_null, facet_wrap
from ._plot_layout_items import PlotLayoutItems
from ._side_space import GridSpecParams, _side_space
if TYPE_CHECKING:
from plotnine import ggplot
from plotnine._mpl.gridspec import p9GridSpec
from plotnine.iapi import outside_legend
class _plot_side_space(_side_space):
"""
Base class for the side space around a plot
"""
def __init__(self, items: PlotLayoutItems):
self.items = items
self.gridspec = items.plot._gridspec
self._calculate()
@cached_property
def _legend_size(self) -> tuple[float, float]:
"""
Return size of legend in figure coordinates
We need this to accurately justify the legend by proportional
values e.g. 0.2, instead of just left, right, top, bottom &
center.
"""
if not self.has_legend:
return (0, 0)
ol: outside_legend = getattr(self.items.legends, self.side)
return self.items.geometry.size(ol.box)
@cached_property
def legend_width(self) -> float:
"""
Return width of legend in figure coordinates
"""
return self._legend_size[0]
@cached_property
def legend_height(self) -> float:
"""
Return height of legend in figure coordinates
"""
return self._legend_size[1]
@property
def has_tag(self) -> bool:
"""
Return True if the space/margin to this side of the panel has a tag
If it does, then it will be included in the layout
"""
getp = self.items.plot.theme.getp
return getp("plot_tag_location") == "margin" and self.side in getp(
"plot_tag_position"
)
@property
def has_legend(self) -> bool:
"""
Return True if the space/margin to this side of the panel has a legend
If it does, then it will be included in the layout
"""
if not self.items.legends:
return False
return hasattr(self.items.legends, self.side)
@property
def tag_width(self) -> float:
"""
The width of the tag including the margins
The value is zero except if all these are true:
- The tag is in the margin `theme(plot_tag_position = "margin")`
- The tag at one one of the the following locations;
left, right, topleft, topright, bottomleft or bottomright
"""
return 0
@property
def tag_height(self) -> float:
"""
The height of the tag including the margins
The value is zero except if all these are true:
- The tag is in the margin `theme(plot_tag_position = "margin")`
- The tag at one one of the the following locations;
top, bottom, topleft, topright, bottomleft or bottomright
"""
return 0
@property
def axis_title_clearance(self) -> float:
"""
The distance between the axis title and the panel
Figure
----------------------------
| Panel |
| ----------- |
| | | |
| | | |
| Y<--->| | |
| | | |
| | | |
| ----------- |
| |
----------------------------
We use this value to when aligning axis titles in a
plot composition.
"""
try:
return self.total - self.sum_upto("axis_title_alignment")
except AttributeError as err:
# There is probably an error in in the layout manager
raise PlotnineError("Side has no axis title") from err
class left_space(_plot_side_space):
"""
Space in the figure for artists on the left of the panel area
Ordered from the edge of the figure and going inwards
"""
plot_margin: float = 0
tag_alignment: float = 0
"""
Space added to align the tag in this plot with others in a composition
This value is calculated during the layout process, and it ensures that
all tags on this side of the plot take up the same amount of space in
the margin. e.g. from
------------------------------------
| plot_margin | tag | artists |
|------------------------------------|
| plot_margin | A long tag | artists |
------------------------------------
to
------------------------------------
| plot_margin | tag | artists |
|------------------------------------|
| plot_margin | A long tag | artists |
------------------------------------
And the tag is justified within that space e.g if ha="left" we get
------------------------------------
| plot_margin | tag | artists |
|------------------------------------|
| plot_margin | A long tag | artists |
------------------------------------
So, contrary to the order in which the space items are laid out, the
tag_alignment does not necessarily come before the plot_tag.
"""
plot_tag_margin_left: float = 0
plot_tag: float = 0
plot_tag_margin_right: float = 0
margin_alignment: float = 0
"""
Space added to align this plot with others in a composition
This value is calculated during the layout process in a tree structure
that has convenient access to the sides/edges of the panels in the
composition.
"""
legend: float = 0
legend_box_spacing: float = 0
axis_title_y_margin_left: float = 0
axis_title_y: float = 0
axis_title_y_margin_right: float = 0
axis_title_alignment: float = 0
"""
Space added to align the axis title with others in a composition
This value is calculated during the layout process. The amount is
the difference between the largest and smallest axis_title_clearance
among the items in the composition.
"""
axis_text_y_margin_left: float = 0
axis_text_y: float = 0
axis_text_y_margin_right: float = 0
axis_ticks_y: float = 0
def _calculate(self):
theme = self.items.plot.theme
geometry = self.items.geometry
items = self.items
self.plot_margin = theme.getp("plot_margin_left")
if self.has_tag and items.plot_tag:
m = theme.get_margin("plot_tag").fig
self.plot_tag_margin_left = m.l
self.plot_tag = geometry.width(items.plot_tag)
self.plot_tag_margin_right = m.r
if items.legends and items.legends.left:
self.legend = self.legend_width
self.legend_box_spacing = theme.getp("legend_box_spacing")
if items.axis_title_y:
m = theme.get_margin("axis_title_y").fig
self.axis_title_y_margin_left = m.l
self.axis_title_y = geometry.width(items.axis_title_y)
self.axis_title_y_margin_right = m.r
# Account for the space consumed by the axis
self.axis_text_y = items.axis_text_y_max_width_at("first_col")
if self.axis_text_y:
m = theme.get_margin("axis_text_y").fig
self.axis_text_y_margin_left = m.l
self.axis_text_y_margin_right = m.r
self.axis_ticks_y = items.axis_ticks_y_max_width_at("first_col")
# Adjust plot_margin to make room for ylabels that protude well
# beyond the axes
# NOTE: This adjustment breaks down when the protrusion is large
protrusion = items.axis_text_x_left_protrusion("all")
adjustment = protrusion - (self.total - self.plot_margin)
if adjustment > 0:
self.plot_margin += adjustment
@property
def offset(self) -> float:
"""
Distance from left of the figure to the left of the plot gridspec
----------------(1, 1)
| ---- |
| dx | | |
|<--->| | |
| | | |
| ---- |
(0, 0)----------------
"""
return self.gridspec.bbox_relative.x0
def x1(self, item: str) -> float:
"""
Lower x-coordinate in figure space of the item
"""
return self.to_figure_space(self.sum_upto(item))
def x2(self, item: str) -> float:
"""
Higher x-coordinate in figure space of the item
"""
return self.to_figure_space(self.sum_incl(item))
@property
def panel_left_relative(self):
"""
Left (relative to the gridspec) of the panels in figure dimensions
"""
return self.total
@property
def panel_left(self):
"""
Left of the panels in figure space
"""
return self.to_figure_space(self.panel_left_relative)
@property
def plot_left(self):
"""
Distance up to the left-most artist in figure space
"""
return self.x1("legend")
@property
def tag_width(self):
"""
The width of the tag including the margins
"""
return (
self.plot_tag_margin_left
+ self.plot_tag
+ self.plot_tag_margin_right
)
class right_space(_plot_side_space):
"""
Space in the figure for artists on the right of the panel area
Ordered from the edge of the figure and going inwards
"""
plot_margin: float = 0
tag_alignment: float = 0
plot_tag_margin_right: float = 0
plot_tag: float = 0
plot_tag_margin_left: float = 0
margin_alignment: float = 0
legend: float = 0
legend_box_spacing: float = 0
strip_text_y_extra_width: float = 0
def _calculate(self):
items = self.items
theme = self.items.plot.theme
geometry = self.items.geometry
self.plot_margin = theme.getp("plot_margin_right")
if self.has_tag and items.plot_tag:
m = theme.get_margin("plot_tag").fig
self.plot_tag_margin_right = m.r
self.plot_tag = geometry.width(items.plot_tag)
self.plot_tag_margin_left = m.l
if items.legends and items.legends.right:
self.legend = self.legend_width
self.legend_box_spacing = theme.getp("legend_box_spacing")
self.strip_text_y_extra_width = items.strip_text_y_extra_width("right")
# Adjust plot_margin to make room for ylabels that protude well
# beyond the axes
# NOTE: This adjustment breaks down when the protrusion is large
protrusion = items.axis_text_x_right_protrusion("all")
adjustment = protrusion - (self.total - self.plot_margin)
if adjustment > 0:
self.plot_margin += adjustment
@property
def offset(self):
"""
Distance from right of the figure to the right of the plot gridspec
---------------(1, 1)
| ---- |
| | | -dx |
| | |<--->|
| | | |
| ---- |
(0, 0)---------------
"""
return self.gridspec.bbox_relative.x1 - 1
def x1(self, item: str) -> float:
"""
Lower x-coordinate in figure space of the item
"""
return self.to_figure_space(1 - self.sum_incl(item))
def x2(self, item: str) -> float:
"""
Higher x-coordinate in figure space of the item
"""
return self.to_figure_space(1 - self.sum_upto(item))
@property
def panel_right_relative(self):
"""
Right (relative to the gridspec) of the panels in figure dimensions
"""
return 1 - self.total
@property
def panel_right(self):
"""
Right of the panels in figure space
"""
return self.to_figure_space(self.panel_right_relative)
@property
def plot_right(self):
"""
Distance up to the right-most artist in figure space
"""
return self.x2("legend")
@property
def tag_width(self):
"""
The width of the tag including the margins
"""
return (
self.plot_tag_margin_right
+ self.plot_tag
+ self.plot_tag_margin_left
)
class top_space(_plot_side_space):
"""
Space in the figure for artists above the panel area
Ordered from the edge of the figure and going inwards
"""
plot_margin: float = 0
tag_alignment: float = 0
plot_tag_margin_top: float = 0
plot_tag: float = 0
plot_tag_margin_bottom: float = 0
margin_alignment: float = 0
plot_title_margin_top: float = 0
plot_title: float = 0
plot_title_margin_bottom: float = 0
plot_subtitle_margin_top: float = 0
plot_subtitle: float = 0
plot_subtitle_margin_bottom: float = 0
legend: float = 0
legend_box_spacing: float = 0
strip_text_x_extra_height: float = 0
def _calculate(self):
items = self.items
theme = self.items.plot.theme
geometry = self.items.geometry
W, H = theme.getp("figure_size")
F = W / H
self.plot_margin = theme.getp("plot_margin_top") * F
if self.has_tag and items.plot_tag:
m = theme.get_margin("plot_tag").fig
self.plot_tag_margin_top = m.t
self.plot_tag = geometry.height(items.plot_tag)
self.plot_tag_margin_bottom = m.b
if items.plot_title:
m = theme.get_margin("plot_title").fig
self.plot_title_margin_top = m.t
self.plot_title = geometry.height(items.plot_title)
self.plot_title_margin_bottom = m.b
if items.plot_subtitle:
m = theme.get_margin("plot_subtitle").fig
self.plot_subtitle_margin_top = m.t
self.plot_subtitle = geometry.height(items.plot_subtitle)
self.plot_subtitle_margin_bottom = m.b
if items.legends and items.legends.top:
self.legend = self.legend_height
self.legend_box_spacing = theme.getp("legend_box_spacing") * F
self.strip_text_x_extra_height = items.strip_text_x_extra_height("top")
# Adjust plot_margin to make room for ylabels that protude well
# beyond the axes
# NOTE: This adjustment breaks down when the protrusion is large
protrusion = items.axis_text_y_top_protrusion("all")
adjustment = protrusion - (self.total - self.plot_margin)
if adjustment > 0:
self.plot_margin += adjustment
@property
def offset(self) -> float:
"""
Distance from top of the figure to the top of the plot gridspec
----------------(1, 1)
| ^ |
| |-dy |
| v |
| ---- |
| | | |
| | | |
| | | |
| ---- |
| |
(0, 0)----------------
"""
return self.gridspec.bbox_relative.y1 - 1
def y1(self, item: str) -> float:
"""
Lower y-coordinate in figure space of the item
"""
return self.to_figure_space(1 - self.sum_incl(item))
def y2(self, item: str) -> float:
"""
Higher y-coordinate in figure space of the item
"""
return self.to_figure_space(1 - self.sum_upto(item))
@property
def panel_top_relative(self):
"""
Top (relative to the gridspec) of the panels in figure dimensions
"""
return 1 - self.total
@property
def panel_top(self):
"""
Top of the panels in figure space
"""
return self.to_figure_space(self.panel_top_relative)
@property
def plot_top(self):
"""
Distance up to the top-most artist in figure space
"""
return self.y2("legend")
@property
def tag_height(self):
"""
The height of the tag including the margins
"""
return (
self.plot_tag_margin_top
+ self.plot_tag
+ self.plot_tag_margin_bottom
)
class bottom_space(_plot_side_space):
"""
Space in the figure for artists below the panel area
Ordered from the edge of the figure and going inwards
"""
plot_footer_margin_bottom: float = 0
plot_footer: float = 0
plot_footer_margin_top: float = 0
plot_margin: float = 0
tag_alignment: float = 0
plot_tag_margin_bottom: float = 0
plot_tag: float = 0
plot_tag_margin_top: float = 0
margin_alignment: float = 0
plot_caption_margin_bottom: float = 0
plot_caption: float = 0
plot_caption_margin_top: float = 0
legend: float = 0
legend_box_spacing: float = 0
axis_title_x_margin_bottom: float = 0
axis_title_x: float = 0
axis_title_x_margin_top: float = 0
axis_title_alignment: float = 0
"""
Space added to align the axis title with others in a composition
This value is calculated during the layout process in a tree structure
that has convenient access to the sides/edges of the panels in the
composition. It's amount is the difference in height between this axis
text (and it's margins) and the tallest axis text (and it's margin).
"""
axis_text_x_margin_bottom: float = 0
axis_text_x: float = 0
axis_text_x_margin_top: float = 0
axis_ticks_x: float = 0
def _calculate(self):
items = self.items
theme = self.items.plot.theme
geometry = self.items.geometry
W, H = theme.getp("figure_size")
F = W / H
self.plot_margin = theme.getp("plot_margin_bottom") * F
if self.has_tag and items.plot_tag:
m = theme.get_margin("plot_tag").fig
self.plot_tag_margin_bottom = m.b
self.plot_tag = geometry.height(items.plot_tag)
self.plot_tag_margin_top = m.t
if items.plot_caption:
m = theme.get_margin("plot_caption").fig
self.plot_caption_margin_bottom = m.b
self.plot_caption = geometry.height(items.plot_caption)
self.plot_caption_margin_top = m.t
if items.plot_footer:
m = theme.get_margin("plot_footer").fig
self.plot_footer_margin_bottom = m.b
self.plot_footer = geometry.height(items.plot_footer)
self.plot_footer_margin_top = m.t
if items.legends and items.legends.bottom:
self.legend = self.legend_height
self.legend_box_spacing = theme.getp("legend_box_spacing") * F
if items.axis_title_x:
m = theme.get_margin("axis_title_x").fig
self.axis_title_x_margin_bottom = m.b
self.axis_title_x = geometry.height(items.axis_title_x)
self.axis_title_x_margin_top = m.t
# Account for the space consumed by the axis
self.axis_text_x = items.axis_text_x_max_height_at("last_row")
if self.axis_text_x:
m = theme.get_margin("axis_text_x").fig
self.axis_text_x_margin_bottom = m.b
self.axis_text_x_margin_top = m.t
self.axis_ticks_x = items.axis_ticks_x_max_height_at("last_row")
# Adjust plot_margin to make room for ylabels that protude well
# beyond the axes
# NOTE: This adjustment breaks down when the protrusion is large
protrusion = items.axis_text_y_bottom_protrusion("all")
adjustment = protrusion - (self.total - self.plot_margin)
if adjustment > 0:
self.plot_margin += adjustment
@property
def offset(self) -> float:
"""
Distance from bottom of the figure to the bottom of the plot gridspec
----------------(1, 1)
| |
| ---- |
| | | |
| | | |
| | | |
| ---- |
| ^ |
| |dy |
| v |
(0, 0)----------------
"""
return self.gridspec.bbox_relative.y0
def y1(self, item: str) -> float:
"""
Lower y-coordinate in figure space of the item
"""
return self.to_figure_space(self.sum_upto(item))
def y2(self, item: str) -> float:
"""
Higher y-coordinate in figure space of the item
"""
return self.to_figure_space(self.sum_incl(item))
@property
def panel_bottom_relative(self):
"""
Bottom (relative to the gridspec) of the panels in figure dimensions
"""
return self.total
@property
def panel_bottom(self):
"""
Bottom of the panels in figure space
"""
return self.to_figure_space(self.panel_bottom_relative)
@property
def plot_bottom(self):
"""
Distance up to the bottom-most artist in figure space
"""
return self.y1("legend")
@property
def footer_height(self):
"""
The height of the footer including the margins
"""
return (
self.plot_footer_margin_bottom
+ self.plot_footer
+ self.plot_footer_margin_top
)
@property
def tag_height(self):
"""
The height of the tag including the margins
"""
return (
self.plot_tag_margin_bottom
+ self.plot_tag
+ self.plot_tag_margin_top
)
class PlotSideSpaces:
"""
Compute the all the spaces required in the layout
These are:
1. The space of each artist between the panel and the edge of the
figure.
2. The space in-between the panels
From these values, we put together the grid-spec parameters required
by matplotblib to position the axes. We also use the values to adjust
the coordinates of all the artists that occupy these spaces, placing
them in their final positions.
"""
W: float
"""Figure Width [inches]"""
H: float
"""Figure Height [inches]"""
w: float
"""Axes width w.r.t figure in [0, 1]"""
h: float
"""Axes height w.r.t figure in [0, 1]"""
sh: float
"""horizontal spacing btn panels w.r.t figure"""
sw: float
"""vertical spacing btn panels w.r.t figure"""
def __init__(self, plot: ggplot):
self.plot = plot
self.gridspec = plot._gridspec
self.sub_gridspec = plot._sub_gridspec
self.items = PlotLayoutItems(plot)
self.l = left_space(self.items)
"""All subspaces to the left of the panels"""
self.r = right_space(self.items)
"""All subspaces to the right of the panels"""
self.t = top_space(self.items)
"""All subspaces above the top of the panels"""
self.b = bottom_space(self.items)
"""All subspaces below the bottom of the panels"""
self.W, self.H = plot.theme.getp("figure_size")
@property
def owner(self) -> ggplot:
"""
The plot these side-spaces are calculated against
"""
return self.plot
def arrange(self):
"""
Resize plot and place artists in final positions around the panels
"""
self.resize_gridspec()
self.items._move_artists(self)
self._arrange_insets()
def _arrange_insets(self):
"""
Position and arrange every inset attached to this plot
The host's panel/plot/full region is now finalised, so the
inset's fractional bounding box is scaled into figure
coordinates. For ggplot / Compose insets the bbox drives the
inset's gridspec and its side-space layout runs to position
its content. For image insets the adapter's `_arrange_in_box`
does the aspect-fit math directly — no gridspec or side-space
work needed.
"""
from plotnine import ggplot
from plotnine.composition import Compose
from plotnine.composition._inset_image import _InsetImage
from ._composition_side_space import CompositionSideSpaces
for inset in self.plot._insets:
if inset.align_to == "panel":
(x1, y1), (x2, y2) = self.panel_area_coordinates
elif inset.align_to == "plot":
(x1, y1), (x2, y2) = self.plot_area_coordinates
elif inset.align_to == "footer":
if not self.plot.labels.get("footer", ""):
continue
(x1, y1), (x2, y2) = self.footer_area_coordinates
else: # "full"
# Note that this isn't necessarily the figure's coordinates,
# rather the entire ggplot area.
bbox = self.plot._gridspec.bbox_relative
(x1, y1), (x2, y2) = (bbox.x0, bbox.y0), (bbox.x1, bbox.y1)
left = x1 + inset.left * (x2 - x1)
bottom = y1 + inset.bottom * (y2 - y1)
right = x1 + inset.right * (x2 - x1)
top = y1 + inset.top * (y2 - y1)
if isinstance(inset.obj, (ggplot, Compose)):
params = GridSpecParams(
left=left,
bottom=bottom,
right=right,
top=top,
wspace=0,
hspace=0,
)
inset.obj._gridspec.update_params_and_artists(params)
if isinstance(inset.obj, ggplot):
inset.obj._sidespaces = PlotSideSpaces(inset.obj)
else:
inset.obj._sidespaces = CompositionSideSpaces(inset.obj)
inset.obj._sidespaces.arrange()
elif isinstance(inset.obj, _InsetImage):
inset.obj._arrange_in_box(left, bottom, right, top)
def resize_gridspec(self):
"""
Apply the space calculations to the sub_gridspec
After calling this method, the sub_gridspec will be appropriately
sized to accomodate the artists around the panels.
"""
gsparams = self.calculate_gridspec_params()
self.sub_gridspec.update_params_and_artists(gsparams)
def calculate_gridspec_params(self) -> GridSpecParams:
"""
Grid spacing between panels w.r.t figure
"""
gsparams = self._calculate_panel_spacing()
# Adjust the spacing parameters for the desired aspect ratio
# It is simpler to adjust for the aspect ratio than to calculate
# the final parameters that are true to the aspect ratio in
# one-short
if (ratio := self.plot.facet._aspect_ratio()) is not None:
current_ratio = self.aspect_ratio
if ratio > current_ratio:
# Increase aspect ratio, taller panels
gsparams = self._reduce_width(gsparams, ratio)
elif ratio < current_ratio:
# Increase aspect ratio, wider panels
gsparams = self._reduce_height(gsparams, ratio)
return gsparams
@property
def plot_width(self) -> float:
"""
Width [figure dimensions] of the whole plot
"""
return float(self.gridspec.width)
@property
def plot_height(self) -> float:
"""
Height [figure dimensions] of the whole plot
"""
return float(self.gridspec.height)
@property
def panel_width(self) -> float:
"""
Width [figure dimensions] of panels
"""
return self.r.panel_right - self.l.panel_left
@property
def panel_height(self) -> float:
"""
Height [figure dimensions] of panels
"""
return self.t.panel_top - self.b.panel_bottom
@property
def plot_left(self) -> float:
return self.l.plot_left
@property
def plot_right(self) -> float:
return self.r.plot_right
@property
def plot_bottom(self) -> float:
return self.b.plot_bottom
@property
def plot_top(self) -> float:
return self.t.plot_top
@property
def panel_left(self) -> float:
return self.l.panel_left
@property
def panel_right(self) -> float:
return self.r.panel_right
@property
def panel_bottom(self) -> float:
return self.b.panel_bottom
@property
def panel_top(self) -> float:
return self.t.panel_top
@property
def horizontal_space(self) -> float:
"""
Horizontal non-panel space [figure dimensions]
"""
# The same as plot_width - panel_width
return self.l.total + self.r.total
@property
def vertical_space(self) -> float:
"""
Vertical non-panel space [figure dimensions]
"""
# The same as plot_height - panel_height
return self.t.total + self.b.total
def increase_horizontal_plot_margin(self, dw: float):
"""
Increase the plot_margin to the right & left of the panels
"""
self.l.plot_margin += dw
self.r.plot_margin += dw
def increase_vertical_plot_margin(self, dh: float):
"""
Increase the plot_margin to the above & below of the panels
"""
self.t.plot_margin += dh
self.b.plot_margin += dh
@property
def plot_area_coordinates(
self,
) -> tuple[tuple[float, float], tuple[float, float]]:
"""
Lower-left and upper-right coordinates of the plot area
This is the area surrounded by the plot_margin.
"""
x1, x2 = self.l.x2("plot_margin"), self.r.x1("plot_margin")
y1, y2 = self.b.y2("plot_margin"), self.t.y1("plot_margin")
return ((x1, y1), (x2, y2))
@property
def panel_area_coordinates(
self,
) -> tuple[tuple[float, float], tuple[float, float]]:
"""
Lower-left and upper-right coordinates of the panel area
This is the area in which the panels are drawn.
"""
x1, x2 = self.l.panel_left, self.r.panel_right
y1, y2 = self.b.panel_bottom, self.t.panel_top
return ((x1, y1), (x2, y2))
@property
def footer_area_coordinates(
self,
) -> tuple[tuple[float, float], tuple[float, float]]:
"""
Lower-left and upper-right coordinates of the footer band
This is the strip reserved at the bottom of the figure for the
plot footer text and its margins. It spans the full plot width.
It has zero height when there is no footer text.
"""
# Full plot width, matching where the footer background is drawn
x1 = self.l.offset