Merge pull request #32346 from rcomer/fig-anchoredbbox · matplotlib/matplotlib@ed319f7 · GitHub
Skip to content

Commit ed319f7

Browse files
authored
Merge pull request #32346 from rcomer/fig-anchoredbbox
ENH: AnchoredOffsetbox for (Sub)Figures
2 parents cd3055f + 569ed89 commit ed319f7

4 files changed

Lines changed: 68 additions & 8 deletions

File tree

Lines changed: 32 additions & 0 deletions

‎galleries/examples/misc/anchored_artists.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@
2727
from matplotlib.patches import Circle, Ellipse
2828

2929

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

3737

3838
def draw_circles(ax):
@@ -73,7 +73,7 @@ def draw_sizebar(ax):
7373
fig, ax = plt.subplots()
7474
ax.set_aspect(1)
7575

76-
draw_text(ax)
76+
draw_text(fig)
7777
draw_circles(ax)
7878
draw_ellipse(ax)
7979
draw_sizebar(ax)

‎lib/matplotlib/offsetbox.py‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -909,10 +909,14 @@ class AnchoredOffsetbox(OffsetBox):
909909
"""
910910
An OffsetBox placed according to location *loc*.
911911
912-
AnchoredOffsetbox has a single child. When multiple children are needed,
913-
use an extra OffsetBox to enclose them. By default, the offset box is
914-
anchored against its parent Axes. You may explicitly specify the
915-
*bbox_to_anchor*.
912+
AnchoredOffsetbox has a single child. When multiple children are needed, use an
913+
extra OffsetBox to enclose them. By default, the offset box is anchored against its
914+
parent Axes, SubFigure or Figure. You may explicitly specify the *bbox_to_anchor*.
915+
916+
.. versionadded:: 3.12
917+
`.AnchoredOffsetbox` can now be added directly to a `.Figure` or `.SubFigure`,
918+
and will by default be anchored to that parent.
919+
916920
"""
917921
zorder = 5 # zorder of the legend
918922

@@ -1024,7 +1028,8 @@ def get_bbox(self, renderer):
10241028
def get_bbox_to_anchor(self):
10251029
"""Return the bbox that the box is anchored to."""
10261030
if self._bbox_to_anchor is None:
1027-
return self.axes.bbox
1031+
return (self.axes.bbox if self.axes is not None else
1032+
self.get_figure(root=False).bbox)
10281033
else:
10291034
transform = self._bbox_to_anchor_transform
10301035
if transform is None:

‎lib/matplotlib/tests/test_offsetbox.py‎

Lines changed: 23 additions & 0 deletions

0 commit comments

Comments
 (0)