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

Skip to content

Commit 29b578c

Browse files
committed
TST : simplified annotation bounding box tests
1 parent 50657f7 commit 29b578c

File tree

1 file changed

+30
-18
lines changed

1 file changed

+30
-18
lines changed

lib/matplotlib/tests/test_text.py

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
import numpy as np
88
from numpy.testing import assert_almost_equal
9-
from nose import SkipTest
10-
from nose.tools import assert_raises, eq_
9+
from nose.tools import eq_
1110

11+
from matplotlib.transforms import Bbox
1212
import matplotlib
1313
import matplotlib.pyplot as plt
1414
from matplotlib.testing.decorators import image_comparison, cleanup
@@ -289,6 +289,7 @@ def test_annotation_negative_coords():
289289
ax.annotate("-apx", (-25, -10), xycoords="axes pixels")
290290

291291

292+
@cleanup
292293
def test_text_annotation_get_window_extent():
293294
figure = Figure(dpi=100)
294295
renderer = RendererAgg(200, 200, 100)
@@ -320,32 +321,42 @@ def test_text_annotation_get_window_extent():
320321
eq_(points[1, 1], text_bbox.height - below_line)
321322

322323

324+
@cleanup
323325
def test_text_with_arrow_annotation_get_window_extent():
324-
figure = Figure(dpi=100)
325-
renderer = RendererAgg(600, 600, 100)
326326
headwidth = 21
327-
328-
text = Text(text='test', x=0, y=0)
329-
text.set_figure(figure)
330-
text_bbox = text.get_window_extent(renderer=renderer)
331-
332-
# Text annotation with arrow
333-
annotation = Annotation(
334-
'test',
335-
xy=(0.0, 50.0 + (headwidth / 0.72) * 0.5),
327+
fig, ax = plt.subplots(dpi=100)
328+
txt = ax.text(s='test', x=0, y=0)
329+
ann = ax.annotate('test',
330+
xy=(0.0, 50.0),
336331
xytext=(50.0, 50.0), xycoords='figure pixels',
337332
arrowprops={
338333
'facecolor': 'black', 'width': 2,
339334
'headwidth': headwidth, 'shrink': 0.0})
340-
annotation.set_figure(figure)
341-
annotation.draw(renderer)
342335

343-
bbox = annotation.get_window_extent(renderer=renderer)
336+
plt.draw()
337+
renderer = fig.canvas.renderer
338+
# bounding box of text
339+
text_bbox = txt.get_window_extent(renderer=renderer)
340+
# bounding box of annotation (text + arrow)
341+
bbox = ann.get_window_extent(renderer=renderer)
342+
# bounding box of arrow
343+
arrow_bbox = ann.arrow.get_window_extent(renderer)
344+
# bounding box of annotation text
345+
ann_txt_bbox = Text.get_window_extent(ann)
346+
347+
# make sure annotation with in 50 px wider than
348+
# just the text
344349
eq_(bbox.width, text_bbox.width + 50.0)
345-
expected_height = max(text_bbox.height, headwidth / 0.72)
346-
assert_almost_equal(bbox.height, expected_height)
350+
# make sure the annotation text bounding box is same size
351+
# as the bounding box of the same string as a Text object
352+
eq_(ann_txt_bbox.height, text_bbox.height)
353+
eq_(ann_txt_bbox.width, text_bbox.width)
354+
# compute the expected bounding box of arrow + text
355+
expected_bbox = Bbox.union([ann_txt_bbox, arrow_bbox])
356+
assert_almost_equal(bbox.height, expected_bbox.height)
347357

348358

359+
@cleanup
349360
def test_arrow_annotation_get_window_extent():
350361
figure = Figure(dpi=100)
351362
figure.set_figwidth(2.0)
@@ -369,6 +380,7 @@ def test_arrow_annotation_get_window_extent():
369380
eq_(points[0, 1], 50.0 - 5 / 0.72)
370381

371382

383+
@cleanup
372384
def test_empty_annotation_get_window_extent():
373385
figure = Figure(dpi=100)
374386
figure.set_figwidth(2.0)

0 commit comments

Comments
 (0)