ENH: AnchoredOffsetbox for (Sub)Figures · matplotlib/matplotlib@a2d500e · GitHub
Skip to content

Commit a2d500e

Browse files
committed
ENH: AnchoredOffsetbox for (Sub)Figures
1 parent 1aee967 commit a2d500e

4 files changed

Lines changed: 63 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
@@ -22,12 +22,12 @@
2222
from matplotlib.patches import Circle, Ellipse
2323

2424

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

3232

3333
def draw_circles(ax):
@@ -68,7 +68,7 @@ def draw_sizebar(ax):
6868
fig, ax = plt.subplots()
6969
ax.set_aspect(1)
7070

71-
draw_text(ax)
71+
draw_text(fig)
7272
draw_circles(ax)
7373
draw_ellipse(ax)
7474
draw_sizebar(ax)

lib/matplotlib/offsetbox.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -909,10 +909,9 @@ 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*.
916915
"""
917916
zorder = 5 # zorder of the legend
918917

@@ -1024,7 +1023,8 @@ def get_bbox(self, renderer):
10241023
def get_bbox_to_anchor(self):
10251024
"""Return the bbox that the box is anchored to."""
10261025
if self._bbox_to_anchor is None:
1027-
return self.axes.bbox
1026+
return (self.axes.bbox if self.axes is not None else
1027+
self.get_figure(root=False).bbox)
10281028
else:
10291029
transform = self._bbox_to_anchor_transform
10301030
if transform is None:

lib/matplotlib/tests/test_offsetbox.py

Lines changed: 23 additions & 0 deletions

0 commit comments

Comments
 (0)