Skip to content

Minor cleanup and add test for offsetbox #24486

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
65 changes: 61 additions & 4 deletions lib/matplotlib/tests/test_offsetbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
from numpy.testing import assert_allclose
import pytest

from matplotlib.testing.decorators import image_comparison
from matplotlib.testing.decorators import check_figures_equal, image_comparison
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
import matplotlib.lines as mlines
from matplotlib.backend_bases import MouseButton, MouseEvent

from matplotlib.offsetbox import (
AnchoredOffsetbox, AnnotationBbox, AnchoredText, DrawingArea, OffsetBox,
OffsetImage, PaddedBox, TextArea, _get_packed_offsets, HPacker, VPacker)
AnchoredOffsetbox, AnnotationBbox, AnchoredText, DrawingArea, HPacker,
OffsetBox, OffsetImage, PaddedBox, TextArea, VPacker, _get_packed_offsets)


@image_comparison(['offsetbox_clipping'], remove_text=True)
Expand All @@ -28,6 +28,7 @@ def test_offsetbox_clipping():
fig, ax = plt.subplots()
size = 100
da = DrawingArea(size, size, clip=True)
assert da.clip_children
bg = mpatches.Rectangle((0, 0), size, size,
facecolor='#CCCCCC',
edgecolor='None',
Expand Down Expand Up @@ -386,10 +387,66 @@ def test_packers(align):
[(px + x_height, py), (px, py - y2)])


def test_paddedbox():
def test_paddedbox_default_values():
# smoke test paddedbox for correct default value
fig, ax = plt.subplots()
at = AnchoredText("foo", 'upper left')
pb = PaddedBox(at, patch_attrs={'facecolor': 'r'}, draw_frame=True)
ax.add_artist(pb)
fig.draw_without_rendering()


def test_annotationbbox_properties():
ab = AnnotationBbox(DrawingArea(20, 20, 0, 0, clip=True), (0.5, 0.5),
xycoords='data')
assert ab.xyann == (0.5, 0.5) # xy if xybox not given
assert ab.anncoords == 'data' # xycoords if boxcoords not given

ab = AnnotationBbox(DrawingArea(20, 20, 0, 0, clip=True), (0.5, 0.5),
xybox=(-0.2, 0.4), xycoords='data',
boxcoords='axes fraction')
assert ab.xyann == (-0.2, 0.4) # xybox if given
assert ab.anncoords == 'axes fraction' # boxcoords if given


def test_textarea_properties():
ta = TextArea('Foo')
assert ta.get_text() == 'Foo'
assert not ta.get_multilinebaseline()

ta.set_text('Bar')
ta.set_multilinebaseline(True)
assert ta.get_text() == 'Bar'
assert ta.get_multilinebaseline()


@check_figures_equal()
def test_textarea_set_text(fig_test, fig_ref):
ax_ref = fig_ref.add_subplot()
text0 = AnchoredText("Foo", "upper left")
ax_ref.add_artist(text0)

ax_test = fig_test.add_subplot()
text1 = AnchoredText("Bar", "upper left")
ax_test.add_artist(text1)
text1.txt.set_text("Foo")


@image_comparison(['paddedbox.png'], remove_text=True, style='mpl20')
def test_paddedbox():
fig, ax = plt.subplots()

ta = TextArea("foo")
pb = PaddedBox(ta, pad=5, patch_attrs={'facecolor': 'r'}, draw_frame=True)
ab = AnchoredOffsetbox('upper left', child=pb)
ax.add_artist(ab)

ta = TextArea("bar")
pb = PaddedBox(ta, pad=10, patch_attrs={'facecolor': 'b'})
ab = AnchoredOffsetbox('upper right', child=pb)
ax.add_artist(ab)

ta = TextArea("foobar")
pb = PaddedBox(ta, pad=15, draw_frame=True)
ab = AnchoredOffsetbox('lower right', child=pb)
ax.add_artist(ab)