Skip to content

Commit 9a5f094

Browse files
committed
Arrange insets within the host figure
Adds PlotSideSpaces._arrange_insets, called as the final step of arrange once the host's panel/plot regions are finalised. Each inset's fractional bounding box is scaled into figure coordinates relative to its align_to region and applied to the inset's own gridspec, then a PlotSideSpaces or CompositionSideSpaces is built and arranged to lay out the inset's internal content within those bounds. Also updates inset_element's docstring to use 'fractional coordinates' instead of 'normalised parent coordinates' for terminology familiar to matplotlib users.
1 parent c4f621f commit 9a5f094

2 files changed

Lines changed: 47 additions & 4 deletions

File tree

plotnine/_mpl/layout_manager/_plot_side_space.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -772,6 +772,49 @@ def arrange(self):
772772
"""
773773
self.resize_gridspec()
774774
self.items._move_artists(self)
775+
self._arrange_insets()
776+
777+
def _arrange_insets(self):
778+
"""
779+
Position and arrange every inset attached to this plot
780+
781+
The host's panel/plot/full region is now finalised, so the
782+
inset's fractional bounding box is scaled into figure
783+
coordinates and applied to the inset's own gridspec. The
784+
inset's side-space layout then runs to lay out its content
785+
within those bounds.
786+
"""
787+
from plotnine import ggplot
788+
789+
from ._composition_side_space import CompositionSideSpaces
790+
791+
for inset in self.plot._insets:
792+
if inset.align_to == "panel":
793+
(x1, y1), (x2, y2) = self.panel_area_coordinates
794+
elif inset.align_to == "plot":
795+
(x1, y1), (x2, y2) = self.plot_area_coordinates
796+
else: # "full"
797+
# Note that this isn't necessarily the figure's coordinates,
798+
# rather the entire ggplot area.
799+
bbox = self.plot._gridspec.bbox_relative
800+
(x1, y1), (x2, y2) = (bbox.x0, bbox.y0), (bbox.x1, bbox.y1)
801+
802+
params = GridSpecParams(
803+
left=x1 + inset.left * (x2 - x1),
804+
bottom=y1 + inset.bottom * (y2 - y1),
805+
right=x1 + inset.right * (x2 - x1),
806+
top=y1 + inset.top * (y2 - y1),
807+
wspace=0,
808+
hspace=0,
809+
)
810+
params.validate()
811+
inset.obj._gridspec.update_params_and_artists(params)
812+
813+
if isinstance(inset.obj, ggplot):
814+
inset.obj._sidespaces = PlotSideSpaces(inset.obj)
815+
else:
816+
inset.obj._sidespaces = CompositionSideSpaces(inset.obj)
817+
inset.obj._sidespaces.arrange()
775818

776819
def resize_gridspec(self):
777820
"""

plotnine/composition/_inset_element.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ class inset_element:
2323
obj :
2424
The object to render as an inset.
2525
left, bottom, right, top :
26-
Bounding box of the inset in normalised parent coordinates,
27-
in the range ``[0, 1]``. The bottom-left corner of the region
28-
selected by `align_to` is ``(0, 0)`` and the top-right is
29-
``(1, 1)``.
26+
Bounding box of the inset as fractional coordinates in the
27+
range ``[0, 1]``, relative to the host region selected by
28+
`align_to`. The bottom-left corner of that region is
29+
``(0, 0)`` and the top-right is ``(1, 1)``.
3030
align_to :
3131
Which region of the host plot the bounding box is relative to:
3232

0 commit comments

Comments
 (0)