|
13 | 13 |
|
14 | 14 | from matplotlib.offsetbox import (
|
15 | 15 | AnchoredOffsetbox, AnnotationBbox, AnchoredText, DrawingArea, OffsetBox,
|
16 |
| - OffsetImage, TextArea, _get_packed_offsets) |
| 16 | + OffsetImage, TextArea, _get_packed_offsets, HPacker, VPacker) |
17 | 17 |
|
18 | 18 |
|
19 | 19 | @image_comparison(['offsetbox_clipping'], remove_text=True)
|
@@ -335,3 +335,46 @@ def test_arrowprops_copied():
|
335 | 335 | arrowprops=arrowprops)
|
336 | 336 | assert ab.arrowprops is not ab
|
337 | 337 | assert arrowprops["relpos"] == (.3, .7)
|
| 338 | + |
| 339 | + |
| 340 | +@pytest.mark.parametrize("align", ["baseline", "bottom", "top", |
| 341 | + "left", "right", "center"]) |
| 342 | +def test_packers(align): |
| 343 | + # set the DPI to match points to make the math easier below |
| 344 | + fig = plt.figure(dpi=72) |
| 345 | + x1, y1 = 10, 30 |
| 346 | + x2, y2 = 20, 60 |
| 347 | + r1 = DrawingArea(x1, y1) |
| 348 | + r2 = DrawingArea(x2, y2) |
| 349 | + |
| 350 | + hpacker = HPacker(children=[r1, r2], pad=0, sep=0, align=align) |
| 351 | + vpacker = VPacker(children=[r1, r2], pad=0, sep=0, align=align) |
| 352 | + renderer = fig.canvas.get_renderer() |
| 353 | + |
| 354 | + # HPacker |
| 355 | + *extents, offset_pairs = hpacker.get_extent_offsets(renderer) |
| 356 | + # width, height, xdescent, ydescent |
| 357 | + assert_allclose((x1 + x2, max(y1, y2), 0, 0), extents) |
| 358 | + # internal element placement |
| 359 | + if align in ("baseline", "left", "bottom"): |
| 360 | + y_height = 0 |
| 361 | + elif align in ("right", "top"): |
| 362 | + y_height = y2 - y1 |
| 363 | + elif align == "center": |
| 364 | + y_height = (y2 - y1) / 2 |
| 365 | + # x-offsets, y-offsets |
| 366 | + assert_allclose([(0, y_height), (x1, 0)], offset_pairs) |
| 367 | + |
| 368 | + # VPacker |
| 369 | + *extents, offset_pairs = vpacker.get_extent_offsets(renderer) |
| 370 | + # width, height, xdescent, ydescent |
| 371 | + assert_allclose([max(x1, x2), y1 + y2, 0, max(y1, y2)], extents) |
| 372 | + # internal element placement |
| 373 | + if align in ("baseline", "left", "bottom"): |
| 374 | + x_height = 0 |
| 375 | + elif align in ("right", "top"): |
| 376 | + x_height = x2 - x1 |
| 377 | + elif align == "center": |
| 378 | + x_height = (x2 - x1) / 2 |
| 379 | + # x-offsets, y-offsets |
| 380 | + assert_allclose([(x_height, 0), (0, -y2)], offset_pairs) |
0 commit comments