|
1 | 1 | from collections import namedtuple
|
| 2 | +import io |
2 | 3 |
|
3 | 4 | import numpy as np
|
4 | 5 | from numpy.testing import assert_allclose
|
@@ -238,3 +239,70 @@ def test_picking(child_type, boxcoords):
|
238 | 239 | calls.clear()
|
239 | 240 | fig.canvas.button_press_event(x, y, MouseButton.LEFT)
|
240 | 241 | 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