|
14 | 14 | from dataclasses import replace |
15 | 15 | from functools import cached_property |
16 | 16 | from typing import TYPE_CHECKING |
| 17 | +from warnings import warn |
17 | 18 |
|
18 | | -from plotnine.exceptions import PlotnineError |
| 19 | +from plotnine.exceptions import PlotnineError, PlotnineWarning |
19 | 20 | from plotnine.facets import facet_grid, facet_null, facet_wrap |
20 | 21 |
|
21 | 22 | from ._plot_layout_items import PlotLayoutItems |
@@ -804,6 +805,18 @@ def _arrange_insets(self): |
804 | 805 | (x1, y1), (x2, y2) = self.panel_area_coordinates |
805 | 806 | elif inset.align_to == "plot": |
806 | 807 | (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 |
807 | 820 | else: # "full" |
808 | 821 | # Note that this isn't necessarily the figure's coordinates, |
809 | 822 | # rather the entire ggplot area. |
@@ -980,6 +993,24 @@ def panel_area_coordinates( |
980 | 993 | y1, y2 = self.b.panel_bottom, self.t.panel_top |
981 | 994 | return ((x1, y1), (x2, y2)) |
982 | 995 |
|
| 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 | + |
983 | 1014 | def _calculate_panel_spacing(self) -> GridSpecParams: |
984 | 1015 | """ |
985 | 1016 | Spacing between the panels (wspace & hspace) |
|
0 commit comments