ENH: AnchoredOffsetbox for (Sub)Figures by rcomer · Pull Request #32346 · matplotlib/matplotlib · GitHub
Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions doc/release/next_whats_new/fig_anchored_offsetbox.rst
6 changes: 3 additions & 3 deletions galleries/examples/misc/anchored_artists.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@
from matplotlib.patches import Circle, Ellipse


def draw_text(ax):
def draw_text(fig):
"""Draw a text-box anchored to the upper-left corner of the figure."""
box = AnchoredOffsetbox(child=TextArea("Figure 1a"),
loc="upper left", frameon=True)
box.patch.set_boxstyle("round,pad=0.,rounding_size=0.2")
ax.add_artist(box)
fig.add_artist(box)


def draw_circles(ax):
Expand Down Expand Up @@ -68,7 +68,7 @@ def draw_sizebar(ax):
fig, ax = plt.subplots()
ax.set_aspect(1)

draw_text(ax)
draw_text(fig)
draw_circles(ax)
draw_ellipse(ax)
draw_sizebar(ax)
Expand Down
15 changes: 10 additions & 5 deletions lib/matplotlib/offsetbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -909,10 +909,14 @@ class AnchoredOffsetbox(OffsetBox):
"""
An OffsetBox placed according to location *loc*.

AnchoredOffsetbox has a single child. When multiple children are needed,
use an extra OffsetBox to enclose them. By default, the offset box is
anchored against its parent Axes. You may explicitly specify the
*bbox_to_anchor*.
AnchoredOffsetbox has a single child. When multiple children are needed, use an
extra OffsetBox to enclose them. By default, the offset box is anchored against its
parent Axes, SubFigure or Figure. You may explicitly specify the *bbox_to_anchor*.

.. versionadded:: 3.12
`.AnchoredOffsetbox` can now be added directly to a `.Figure` or `.SubFigure`,
and will by default be anchored to that parent.

"""
zorder = 5 # zorder of the legend

Expand Down Expand Up @@ -1024,7 +1028,8 @@ def get_bbox(self, renderer):
def get_bbox_to_anchor(self):
"""Return the bbox that the box is anchored to."""
if self._bbox_to_anchor is None:
return self.axes.bbox
return (self.axes.bbox if self.axes is not None else
self.get_figure(root=False).bbox)
else:
transform = self._bbox_to_anchor_transform
if transform is None:
Expand Down
23 changes: 23 additions & 0 deletions lib/matplotlib/tests/test_offsetbox.py
Loading