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

Skip to content

Commit 38abc71

Browse files
authored
Merge pull request #24486 from oscargus/offsetboxtests
Minor cleanup and add test for offsetbox
2 parents a046ee3 + f9e9c34 commit 38abc71

File tree

2 files changed

+61
-4
lines changed

2 files changed

+61
-4
lines changed

lib/matplotlib/tests/test_offsetbox.py

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
from numpy.testing import assert_allclose
66
import pytest
77

8-
from matplotlib.testing.decorators import image_comparison
8+
from matplotlib.testing.decorators import check_figures_equal, image_comparison
99
import matplotlib.pyplot as plt
1010
import matplotlib.patches as mpatches
1111
import matplotlib.lines as mlines
1212
from matplotlib.backend_bases import MouseButton, MouseEvent
1313

1414
from matplotlib.offsetbox import (
15-
AnchoredOffsetbox, AnnotationBbox, AnchoredText, DrawingArea, OffsetBox,
16-
OffsetImage, PaddedBox, TextArea, _get_packed_offsets, HPacker, VPacker)
15+
AnchoredOffsetbox, AnnotationBbox, AnchoredText, DrawingArea, HPacker,
16+
OffsetBox, OffsetImage, PaddedBox, TextArea, VPacker, _get_packed_offsets)
1717

1818

1919
@image_comparison(['offsetbox_clipping'], remove_text=True)
@@ -28,6 +28,7 @@ def test_offsetbox_clipping():
2828
fig, ax = plt.subplots()
2929
size = 100
3030
da = DrawingArea(size, size, clip=True)
31+
assert da.clip_children
3132
bg = mpatches.Rectangle((0, 0), size, size,
3233
facecolor='#CCCCCC',
3334
edgecolor='None',
@@ -386,10 +387,66 @@ def test_packers(align):
386387
[(px + x_height, py), (px, py - y2)])
387388

388389

389-
def test_paddedbox():
390+
def test_paddedbox_default_values():
390391
# smoke test paddedbox for correct default value
391392
fig, ax = plt.subplots()
392393
at = AnchoredText("foo", 'upper left')
393394
pb = PaddedBox(at, patch_attrs={'facecolor': 'r'}, draw_frame=True)
394395
ax.add_artist(pb)
395396
fig.draw_without_rendering()
397+
398+
399+
def test_annotationbbox_properties():
400+
ab = AnnotationBbox(DrawingArea(20, 20, 0, 0, clip=True), (0.5, 0.5),
401+
xycoords='data')
402+
assert ab.xyann == (0.5, 0.5) # xy if xybox not given
403+
assert ab.anncoords == 'data' # xycoords if boxcoords not given
404+
405+
ab = AnnotationBbox(DrawingArea(20, 20, 0, 0, clip=True), (0.5, 0.5),
406+
xybox=(-0.2, 0.4), xycoords='data',
407+
boxcoords='axes fraction')
408+
assert ab.xyann == (-0.2, 0.4) # xybox if given
409+
assert ab.anncoords == 'axes fraction' # boxcoords if given
410+
411+
412+
def test_textarea_properties():
413+
ta = TextArea('Foo')
414+
assert ta.get_text() == 'Foo'
415+
assert not ta.get_multilinebaseline()
416+
417+
ta.set_text('Bar')
418+
ta.set_multilinebaseline(True)
419+
assert ta.get_text() == 'Bar'
420+
assert ta.get_multilinebaseline()
421+
422+
423+
@check_figures_equal()
424+
def test_textarea_set_text(fig_test, fig_ref):
425+
ax_ref = fig_ref.add_subplot()
426+
text0 = AnchoredText("Foo", "upper left")
427+
ax_ref.add_artist(text0)
428+
429+
ax_test = fig_test.add_subplot()
430+
text1 = AnchoredText("Bar", "upper left")
431+
ax_test.add_artist(text1)
432+
text1.txt.set_text("Foo")
433+
434+
435+
@image_comparison(['paddedbox.png'], remove_text=True, style='mpl20')
436+
def test_paddedbox():
437+
fig, ax = plt.subplots()
438+
439+
ta = TextArea("foo")
440+
pb = PaddedBox(ta, pad=5, patch_attrs={'facecolor': 'r'}, draw_frame=True)
441+
ab = AnchoredOffsetbox('upper left', child=pb)
442+
ax.add_artist(ab)
443+
444+
ta = TextArea("bar")
445+
pb = PaddedBox(ta, pad=10, patch_attrs={'facecolor': 'b'})
446+
ab = AnchoredOffsetbox('upper right', child=pb)
447+
ax.add_artist(ab)
448+
449+
ta = TextArea("foobar")
450+
pb = PaddedBox(ta, pad=15, draw_frame=True)
451+
ab = AnchoredOffsetbox('lower right', child=pb)
452+
ax.add_artist(ab)

0 commit comments

Comments
 (0)