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

Skip to content

Offsetbox default arguments #24652

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 15 commits into from
Dec 14, 2022
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
18 changes: 9 additions & 9 deletions lib/matplotlib/offsetbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,15 +369,15 @@ def draw(self, renderer):


class PackerBase(OffsetBox):
def __init__(self, pad=None, sep=None, width=None, height=None,
def __init__(self, pad=0., sep=0., width=None, height=None,
align="baseline", mode="fixed", children=None):
"""
Parameters
----------
pad : float, optional
pad : float, default: 0.0
The boundary padding in points.

sep : float, optional
sep : float, default: 0.0
The spacing between items in points.

width, height : float, optional
Expand Down Expand Up @@ -502,13 +502,13 @@ class PaddedBox(OffsetBox):
"""

@_api.make_keyword_only("3.6", name="draw_frame")
def __init__(self, child, pad=None, draw_frame=False, patch_attrs=None):
def __init__(self, child, pad=0., draw_frame=False, patch_attrs=None):
"""
Parameters
----------
child : `~matplotlib.artist.Artist`
The contained `.Artist`.
pad : float
pad : float, default: 0.0
The padding in points. This will be scaled with the renderer dpi.
In contrast, *width* and *height* are in *pixels* and thus not
scaled.
Expand Down Expand Up @@ -1242,13 +1242,13 @@ def __init__(self, offsetbox, xy,
The position *(x, y)* to place the text at. The coordinate system
is determined by *boxcoords*.

xycoords : str or `.Artist` or `.Transform` or callable or \
(float, float), default: 'data'
xycoords : single or two-tuple of str or `.Artist` or `.Transform` or \
callable, default: 'data'
The coordinate system that *xy* is given in. See the parameter
*xycoords* in `.Annotation` for a detailed description.

boxcoords : str or `.Artist` or `.Transform` or callable or \
(float, float), default: value of *xycoords*
boxcoords : single or two-tuple of str or `.Artist` or `.Transform` \
or callable, default: value of *xycoords*
The coordinate system that *xybox* is given in. See the parameter
*textcoords* in `.Annotation` for a detailed description.

Expand Down
16 changes: 13 additions & 3 deletions lib/matplotlib/tests/test_offsetbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

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


@image_comparison(['offsetbox_clipping'], remove_text=True)
Expand Down Expand Up @@ -347,8 +347,9 @@ def test_packers(align):
r1 = DrawingArea(x1, y1)
r2 = DrawingArea(x2, y2)

hpacker = HPacker(children=[r1, r2], pad=0, sep=0, align=align)
vpacker = VPacker(children=[r1, r2], pad=0, sep=0, align=align)
hpacker = HPacker(children=[r1, r2], align=align)
vpacker = VPacker(children=[r1, r2], align=align)

renderer = fig.canvas.get_renderer()

# HPacker
Expand Down Expand Up @@ -378,3 +379,12 @@ def test_packers(align):
x_height = (x2 - x1) / 2
# x-offsets, y-offsets
assert_allclose([(x_height, 0), (0, -y2)], offset_pairs)


def test_paddedbox():
# 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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test passes on main as well. (As the fact that pad is None is not a problem until later on.)

You need to do:

ax.add_artist(pb)
fig.draw_without_rendering()

If you can also add a similar thing for one of the packers that would be great! Can go in the same test/figure. There is a previous test for the packers that you can just copy the relevant code from, but leave the pad and sep arguments out.

ax.add_artist(pb)
fig.draw_without_rendering()