Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 01cb983

Browse files
ImportanceOfBeingErnestQuLogic
authored andcommitted
windowextent-for-annotationbbox
1 parent 90200c6 commit 01cb983

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

lib/matplotlib/offsetbox.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1544,6 +1544,24 @@ def get_fontsize(self, s=None):
15441544
"""Return the fontsize in points."""
15451545
return self.prop.get_size_in_points()
15461546

1547+
def get_window_extent(self, renderer):
1548+
"""
1549+
get the bounding box in display space.
1550+
"""
1551+
bboxes = [child.get_window_extent(renderer)
1552+
for child in self.get_children()]
1553+
1554+
return Bbox.union(bboxes)
1555+
1556+
def get_tightbbox(self, renderer):
1557+
"""
1558+
get tight bounding box in display space.
1559+
"""
1560+
bboxes = [child.get_tightbbox(renderer)
1561+
for child in self.get_children()]
1562+
1563+
return Bbox.union(bboxes)
1564+
15471565
def update_positions(self, renderer):
15481566
"""
15491567
Update the pixel positions of the annotated point and the text.

lib/matplotlib/tests/test_offsetbox.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from collections import namedtuple
2+
import io
23

34
import numpy as np
45
from numpy.testing import assert_allclose
@@ -238,3 +239,70 @@ def test_picking(child_type, boxcoords):
238239
calls.clear()
239240
fig.canvas.button_press_event(x, y, MouseButton.LEFT)
240241
assert len(calls) == 0
242+
243+
244+
def test_annotationbbox_extents():
245+
plt.rcParams.update(plt.rcParamsDefault)
246+
fig, ax = plt.subplots(figsize=(4, 3), dpi=100)
247+
248+
ax.axis([0, 1, 0, 1])
249+
250+
an1 = ax.annotate("Annotation", xy=(.9, .9), xytext=(1.1, 1.1),
251+
arrowprops=dict(arrowstyle="->"), clip_on=False,
252+
va="baseline", ha="left")
253+
254+
da = DrawingArea(20, 20, 0, 0, clip=True)
255+
p = mpatches.Circle((-10, 30), 32)
256+
da.add_artist(p)
257+
258+
ab3 = AnnotationBbox(da, [.5, .5], xybox=(-0.2, 0.5), xycoords='data',
259+
boxcoords="axes fraction", box_alignment=(0., .5),
260+
arrowprops=dict(arrowstyle="->"))
261+
ax.add_artist(ab3)
262+
263+
im = OffsetImage(np.random.rand(10, 10), zoom=3)
264+
im.image.axes = ax
265+
ab6 = AnnotationBbox(im, (0.5, -.3), xybox=(0, 75),
266+
xycoords='axes fraction',
267+
boxcoords="offset points", pad=0.3,
268+
arrowprops=dict(arrowstyle="->"))
269+
ax.add_artist(ab6)
270+
271+
fig.canvas.draw()
272+
renderer = fig.canvas.get_renderer()
273+
274+
# Test Annotation
275+
bb1w = an1.get_window_extent(renderer)
276+
bb1e = an1.get_tightbbox(renderer)
277+
278+
target1 = [332.9, 242.8, 467.0, 298.9]
279+
assert_allclose(bb1w.extents, target1, atol=2)
280+
assert_allclose(bb1e.extents, target1, atol=2)
281+
282+
# Test AnnotationBbox
283+
bb3w = ab3.get_window_extent(renderer)
284+
bb3e = ab3.get_tightbbox(renderer)
285+
286+
target3 = [-17.6, 129.0, 200.7, 167.9]
287+
assert_allclose(bb3w.extents, target3, atol=2)
288+
assert_allclose(bb3e.extents, target3, atol=2)
289+
290+
bb6w = ab6.get_window_extent(renderer)
291+
bb6e = ab6.get_tightbbox(renderer)
292+
293+
target6 = [180.0, -32.0, 230.0, 92.9]
294+
assert_allclose(bb6w.extents, target6, atol=2)
295+
assert_allclose(bb6e.extents, target6, atol=2)
296+
297+
# Test bbox_inches='tight'
298+
buf = io.BytesIO()
299+
fig.savefig(buf, bbox_inches='tight')
300+
buf.seek(0)
301+
shape = plt.imread(buf).shape
302+
targetshape = (350, 504, 4)
303+
assert_allclose(shape, targetshape, atol=2)
304+
305+
# Simple smoke test for tight_layout, to make sure it does not error out.
306+
fig.canvas.draw()
307+
fig.tight_layout()
308+
fig.canvas.draw()

0 commit comments

Comments
 (0)