Skip to content

Commit fcc8106

Browse files
committed
feat(inset): add align_to="footer" target
1 parent fecdbfb commit fcc8106

2 files changed

Lines changed: 33 additions & 2 deletions

File tree

plotnine/_mpl/layout_manager/_plot_side_space.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414
from dataclasses import replace
1515
from functools import cached_property
1616
from typing import TYPE_CHECKING
17+
from warnings import warn
1718

18-
from plotnine.exceptions import PlotnineError
19+
from plotnine.exceptions import PlotnineError, PlotnineWarning
1920
from plotnine.facets import facet_grid, facet_null, facet_wrap
2021

2122
from ._plot_layout_items import PlotLayoutItems
@@ -804,6 +805,18 @@ def _arrange_insets(self):
804805
(x1, y1), (x2, y2) = self.panel_area_coordinates
805806
elif inset.align_to == "plot":
806807
(x1, y1), (x2, y2) = self.plot_area_coordinates
808+
elif inset.align_to == "footer":
809+
(x1, y1), (x2, y2) = self.footer_area_coordinates
810+
# footer_height is exactly 0.0 when there is no footer text
811+
if (y2 - y1) < 1e-9:
812+
warn(
813+
"An inset with align_to='footer' was placed, but "
814+
"the plot has no footer text so the footer band has "
815+
"no height. The inset will not be shown. Set a "
816+
"footer with labs(footer=...).",
817+
PlotnineWarning,
818+
)
819+
continue
807820
else: # "full"
808821
# Note that this isn't necessarily the figure's coordinates,
809822
# rather the entire ggplot area.
@@ -980,6 +993,24 @@ def panel_area_coordinates(
980993
y1, y2 = self.b.panel_bottom, self.t.panel_top
981994
return ((x1, y1), (x2, y2))
982995

996+
@property
997+
def footer_area_coordinates(
998+
self,
999+
) -> tuple[tuple[float, float], tuple[float, float]]:
1000+
"""
1001+
Lower-left and upper-right coordinates of the footer band
1002+
1003+
This is the strip reserved at the bottom of the figure for the
1004+
plot footer text and its margins. It spans the full plot width.
1005+
It has zero height when there is no footer text.
1006+
"""
1007+
# Full plot width, matching where the footer background is drawn
1008+
x1 = self.l.offset
1009+
x2 = self.l.offset + self.plot_width
1010+
y1 = self.b.offset
1011+
y2 = self.b.offset + self.b.footer_height
1012+
return ((x1, y1), (x2, y2))
1013+
9831014
def _calculate_panel_spacing(self) -> GridSpecParams:
9841015
"""
9851016
Spacing between the panels (wspace & hspace)

plotnine/composition/_inset_element.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ class inset_element:
9898
bottom: float
9999
right: float
100100
top: float
101-
align_to: Literal["panel", "plot", "full"] = "panel"
101+
align_to: Literal["panel", "plot", "full", "footer"] = "panel"
102102
on_top: bool = True
103103
anchor: Anchor = "center"
104104

0 commit comments

Comments
 (0)