Skip to content

Commit 7f28431

Browse files
committed
Minor cleanup and add test for offsetbox
1 parent 5df9e3c commit 7f28431

File tree

2 files changed

+60
-3
lines changed

2 files changed

+60
-3
lines changed

lib/matplotlib/tests/test_offsetbox.py

+60-3
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, 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',
@@ -378,3 +379,59 @@ def test_packers(align):
378379
x_height = (x2 - x1) / 2
379380
# x-offsets, y-offsets
380381
assert_allclose([(x_height, 0), (0, -y2)], offset_pairs)
382+
383+
384+
def test_annotationbbox_properties():
385+
ab = AnnotationBbox(DrawingArea(20, 20, 0, 0, clip=True), (0.5, 0.5),
386+
xycoords='data')
387+
assert ab.xyann == (0.5, 0.5) # xy if xybox not given
388+
assert ab.anncoords == 'data' # xycoords if boxcoords not given
389+
390+
ab = AnnotationBbox(DrawingArea(20, 20, 0, 0, clip=True), (0.5, 0.5),
391+
xybox=(-0.2, 0.4), xycoords='data',
392+
boxcoords='axes fraction')
393+
assert ab.xyann == (-0.2, 0.4) # xybox if given
394+
assert ab.anncoords == 'axes fraction' # boxcoords if given
395+
396+
397+
def test_textarea_properties():
398+
ta = TextArea('Foo')
399+
assert ta.get_text() == 'Foo'
400+
assert not ta.get_multilinebaseline()
401+
402+
ta.set_text('Bar')
403+
ta.set_multilinebaseline(True)
404+
assert ta.get_text() == 'Bar'
405+
assert ta.get_multilinebaseline()
406+
407+
408+
@check_figures_equal()
409+
def test_textarea_set_text(fig_test, fig_ref):
410+
ax_ref = fig_ref.add_subplot()
411+
text0 = AnchoredText("Foo", "upper left")
412+
ax_ref.add_artist(text0)
413+
414+
ax_test = fig_test.add_subplot()
415+
text1 = AnchoredText("Bar", "upper left")
416+
ax_test.add_artist(text1)
417+
text1.txt.set_text("Foo")
418+
419+
420+
@image_comparison(['paddedbox.png'], remove_text=True, style='mpl20')
421+
def test_paddedbox():
422+
fig, ax = plt.subplots()
423+
424+
ta = TextArea("foo")
425+
pb = PaddedBox(ta, pad=5, patch_attrs={'facecolor': 'r'}, draw_frame=True)
426+
ab = AnchoredOffsetbox('upper left', child=pb)
427+
ax.add_artist(ab)
428+
429+
ta = TextArea("bar")
430+
pb = PaddedBox(ta, pad=10, patch_attrs={'facecolor': 'b'})
431+
ab = AnchoredOffsetbox('upper right', child=pb)
432+
ax.add_artist(ab)
433+
434+
ta = TextArea("foobar")
435+
pb = PaddedBox(ta, pad=15, draw_frame=True)
436+
ab = AnchoredOffsetbox('lower right', child=pb)
437+
ax.add_artist(ab)

0 commit comments

Comments
 (0)