8000 windowextent-for-annotationbbox · matplotlib/matplotlib@80bc6f1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 80bc6f1

Browse files
windowextent-for-annotationbbox
1 parent 667a100 commit 80bc6f1

File tree

2 files changed

+88
-2
lines changed

2 files changed

+88
-2
lines changed

lib/matplotlib/offsetbox.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1601,6 +1601,24 @@ def get_fontsize(self, s=None):
16011601
"""
16021602
return self.prop.get_size_in_points()
16031603

1604+
def get_window_extent(self, renderer):
1605+
"""
1606+
get the bounding box in display space.
1607+
"""
1608+
bboxes = [child.get_window_extent(renderer)
1609+
for child in self.get_children()]
1610+
1611+
return Bbox.union(bboxes)
1612+
1613+
def get_tightbbox(self, renderer):
1614+
"""
1615+
get tight bounding box in display space.
1616+
"""
1617+
bboxes = [child.get_tightbbox(renderer)
1618+
for child in self.get_children()]
1619+
1620+
return Bbox.union(bboxes)
1621+
16041622
def update_positions(self, renderer):
16051623
"""
16061624
Update the pixel positions of the annotated point and the text.

lib/matplotlib/tests/test_offsetbox.py

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
from collections import namedtuple
22
import numpy.testing as nptest
33
import pytest
4+
import io
5+
import numpy as np
6+
from numpy.testing import assert_allclose
47
from matplotlib.testing.decorators import image_comparison
58
import matplotlib.pyplot as plt
69
import matplotlib.patches as mpatches
710
import matplotlib.lines as mlines
8-
from matplotlib.offsetbox import (
9-
AnchoredOffsetbox, DrawingArea, _get_packed_offsets)
11+
import matplotlib.transforms as mtransforms
12+
from matplotlib.offsetbox import (AnnotationBbox, OffsetImage,
13+
AnchoredOffsetbox, DrawingArea,
14+
_get_packed_offsets)
1015

1116

1217
@image_comparison(['offsetbox_clipping'], remove_text=True)
@@ -180,3 +185,66 @@ def test_get_packed_offsets_equal(wd_list, total, sep, expected):
180185
def test_get_packed_offsets_equal_total_none_sep_none():
181186
with pytest.raises(ValueError):
182187
_get_packed_offsets([(1, 0)] * 3, total=None, sep=None, mode='equal')
188+
189+
190+
def test_annotationbbox_extents():
191+
plt.rcParams.update(plt.rcParamsDefault)
192+
fig, ax = plt.subplots(figsize=(4, 3), dpi=100)
193+
194+
ax.axis([0, 1, 0, 1])
195+
196+
an1 = ax.annotate("Annotation", xy=(.9, .9), xytext=(1.1, 1.1),
197+
arrowprops=dict(arrowstyle="->"), clip_on=False,
198+
va="baseline", ha="left")
199+
200+
da = DrawingArea(20, 20, 0, 0, clip=True)
201+
p = mpatches.Circle((-10, 30), 32)
202+
da.add_artist(p)
203+
204+
ab3 = AnnotationBbox(da, [.5, .5], xybox=(-0.2, 0.5), xycoords='data',
205+
boxcoords="axes fraction", box_alignment=(0., .5),
206+
arrowprops=dict(arrowstyle="->"))
207+
ax.add_artist(ab3)
208+
209+
im = OffsetImage(np.random.rand(10, 10), zoom=3)
210+
im.image.axes = ax
211+
ab6 = AnnotationBbox(im, (0.5, -.3), xybox=(0, 75),
212+
xycoords='axes fraction',
213+
boxcoords="offset points", pad=0.3,
214+
arrowprops=dict(arrowstyle="->"))
215+
ax.add_artist(ab6)
216+
217+
fig.canvas.draw()
218+
renderer = fig.canvas.get_renderer()
219+
220+
# Test Annotation
221+
bb1w = an1.get_window_extent(renderer)
222+
bb1e = an1.get_tightbbox(renderer)
223+
224+
target1 = [332.9, 242.8, 467.0, 298.9]
225+
assert_allclose(bb1w.extents, target1, atol=2)
226+
assert_allclose(bb1e.extents, target1, atol=2)
227+
228+
# Test AnnotationBbox
229+
bb3w = ab3.get_window_extent(renderer)
230+
bb3e = ab3.get_tightbbox(renderer)
231+
232+
target3 = [-17.6, 129.0, 200.7, 167.9]
233+
assert_allclose(bb3w.extents, target3, atol=2)
234+
assert_allclose(bb3e.extents, target3, atol=2)
235+
236+
bb6w = ab6.get_window_extent(renderer)
237+
bb6e = ab6.get_tightbbox(renderer)
238+
239+
target6 = [180.0, -32.0, 230.0, 92.9]
240+
assert_allclose(bb6w.extents, target6, atol=2)
241+
assert_allclose(bb6e.extents, target6, atol=2)
242+
243+
# Test bbox_inches='tight'
244+
buf = io.BytesIO()
245+
fig.savefig(buf, bbox_inches='tight')
246+
buf.seek(0)
247+
shape = plt.imread(buf).shape
248+
targetshape = (350, 504, 4)
249+
assert_allclose(shape, targetshape, atol=2)
250+

0 commit comments

Comments
 (0)
0