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

Skip to content

Commit e14c083

Browse files
windowextent-for-annotationbbox
1 parent 9f0afd2 commit e14c083

File tree

2 files changed

+102
-2
lines changed

2 files changed

+102
-2
lines changed

lib/matplotlib/offsetbox.py

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

1497+
def get_window_extent(self, renderer):
1498+
"""
1499+
get the bounding box in display space.
1500+
"""
1501+
bboxes = [child.get_window_extent(renderer)
1502+
for child in self.get_children()]
1503+
1504+
return Bbox.union(bboxes)
1505+
1506+
def get_tightbbox(self, renderer):
1507+
"""
1508+
get tight bounding box in display space.
1509+
"""
1510+
bboxes = [child.get_tightbbox(renderer)
1511+
for child in self.get_children()]
1512+
1513+
return Bbox.union(bboxes)
1514+
14971515
def update_positions(self, renderer):
14981516
"""
14991517
Update the pixel positions of the annotated point and the text.

lib/matplotlib/tests/test_offsetbox.py

Lines changed: 84 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
import pytest
2+
import io
3+
import numpy as np
4+
from numpy.testing import assert_allclose
25
from matplotlib.testing.decorators import image_comparison
36
import matplotlib.pyplot as plt
47
imp 8000 ort matplotlib.patches as mpatches
58
import matplotlib.lines as mlines
6-
from matplotlib.offsetbox import (
7-
AnchoredOffsetbox, DrawingArea, _get_packed_offsets)
9+
import matplotlib.transforms as mtransforms
10+
from matplotlib.offsetbox import (AnnotationBbox, OffsetImage,
11+
AnchoredOffsetbox, DrawingArea,
12+
_get_packed_offsets)
813

914

1015
@image_comparison(baseline_images=['offsetbox_clipping'], remove_text=True)
@@ -124,3 +129,80 @@ def test_get_packed_offsets(wd_list, total, sep, mode):
124129
# triggered inside this function when calling higher-level functions
125130
# (e.g. `Axes.legend`).
126131
_get_packed_offsets(wd_list, total, sep, mode=mode)
132+
133+
134+
def test_annotationbbox_extents():
135+
plt.rcParams.update(plt.rcParamsDefault)
136+
fig, ax = plt.subplots(figsize=(4, 3), dpi=100)
137+
138+
ax.axis([0, 1, 0, 1])
139+
140+
an1 = ax.annotate("Annotation", xy=(.9, .9), xytext=(1.1, 1.1),
141+
arrowprops=dict(arrowstyle="->"), clip_on=False,
142+
va="baseline", ha="left")
143+
144+
da = DrawingArea(20, 20, 0, 0, clip=True)
145+
p = mpatches.Circle((-10, 30), 32)
146+
da.add_artist(p)
147+
148+
ab3 = AnnotationBbox(da, [.5, .5], xybox=(-0.2, 0.5), xycoords='data',
149+
boxcoords="axes fraction", box_alignment=(0., .5),
150+
arrowprops=dict(arrowstyle="->"))
151+
ax.add_artist(ab3)
152+
153+
im = OffsetImage(np.random.rand(10, 10), zoom=3)
154+
im.image.axes = ax
155+
ab6 = AnnotationBbox(im, (0.5, -.3), xybox=(0, 75),
156+
xycoords='axes fraction',
157+
boxcoords="offset points", pad=0.3,
158+
arrowprops=dict(arrowstyle="->"))
159+
ax.add_artist(ab6)
160+
161+
fig.canvas.draw()
162+
renderer = fig.canvas.get_renderer()
163+
164+
# Test Annotation
165+
bb1w = an1.get_window_extent(renderer)
166+
bb1e = an1.get_tightbbox(renderer)
167+
168+
target1 = [332.9, 242.8, 467.0, 298.9]
169+
assert_allclose(bb1w.extents, target1, atol=2)
170+
assert_allclose(bb1e.extents, target1, atol=2)
171+
172+
# Test AnnotationBbox
173+
bb3w = ab3.get_window_extent(renderer)
174+
bb3e = ab3.get_tightbbox(renderer)
175+
176+
target3 = [-17.6, 129.0, 200.7, 167.9]
177+
assert_allclose(bb3w.extents, target3, atol=2)
178+
assert_allclose(bb3e.extents, target3, atol=2)
179+
180+
bb6w = ab6.get_window_extent(renderer)
181+
bb6e = ab6.get_tightbbox(renderer)
< 6D40 /td>
182+
183+
target6 = [180.0, -32.0, 230.0, 92.9]
184+
assert_allclose(bb6w.extents, target6, atol=2)
185+
assert_allclose(bb6e.extents, target6, atol=2)
186+
187+
# Test bbox_inches='tight'
188+
buf = io.BytesIO()
189+
fig.savefig(buf, bbox_inches='tight')
190+
buf.seek(0)
191+
shape = plt.imread(buf).shape
192+
targetshape = (350, 504, 4)
193+
assert_allclose(shape, targetshape, atol=2)
194+
195+
196+
#test tight layout (only rough testing if extends are inside the figure
197+
# and greater than axes extends)
198+
fig.tight_layout()
199+
fig.canvas.draw()
200+
bbu = mtransforms.Bbox.union((an1.get_window_extent(renderer),
201+
ab3.get_window_extent(renderer),
202+
ab6.get_window_extent(renderer)))
203+
bbx = ax.get_window_extent(renderer)
204+
205+
assert np.all([bbu.x0 > 0, bbu.x0 < bbx.x0, bbu.x1 < 400, bbu.x1 > bbx.x1,
206+
bbu.y0 > 0, bbu.y0 < bbx.y0, bbu.y1 < 300, bbu.y1 > bbx.y1,
207+
bbu.width > bbx.width, bbu.height > bbx.height])
208+

0 commit comments

Comments
 (0)
0