6
6
7
7
import numpy as np
8
8
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_
11
10
11
+ from matplotlib .transforms import Bbox
12
12
import matplotlib
13
13
import matplotlib .pyplot as plt
14
14
from matplotlib .testing .decorators import image_comparison , cleanup
@@ -289,6 +289,7 @@ def test_annotation_negative_coords():
289
289
ax .annotate ("-apx" , (- 25 , - 10 ), xycoords = "axes pixels" )
290
290
291
291
292
+ @cleanup
292
293
def test_text_annotation_get_window_extent ():
293
294
figure = Figure (dpi = 100 )
294
295
renderer = RendererAgg (200 , 200 , 100 )
@@ -320,32 +321,42 @@ def test_text_annotation_get_window_extent():
320
321
eq_ (points [1 , 1 ], text_bbox .height - below_line )
321
322
322
323
324
+ @cleanup
323
325
def test_text_with_arrow_annotation_get_window_extent ():
324
- figure = Figure (dpi = 100 )
325
- renderer = RendererAgg (600 , 600 , 100 )
326
326
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 ),
336
331
xytext = (50.0 , 50.0 ), xycoords = 'figure pixels' ,
337
332
arrowprops = {
338
333
'facecolor' : 'black' , 'width' : 2 ,
339
334
'headwidth' : headwidth , 'shrink' : 0.0 })
340
- annotation .set_figure (figure )
341
- annotation .draw (renderer )
342
335
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
344
349
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 )
347
357
348
358
359
+ @cleanup
349
360
def test_arrow_annotation_get_window_extent ():
350
361
figure = Figure (dpi = 100 )
351
362
figure .set_figwidth (2.0 )
@@ -369,6 +380,7 @@ def test_arrow_annotation_get_window_extent():
369
380
eq_ (points [0 , 1 ], 50.0 - 5 / 0.72 )
370
381
371
382
383
+ @cleanup
372
384
def test_empty_annotation_get_window_extent ():
373
385
figure = Figure (dpi = 100 )
374
386
figure .set_figwidth (2.0 )
0 commit comments