From 85e7c89e95a2b3581ebfd0b025415a2eb55db894 Mon Sep 17 00:00:00 2001 From: Ali Meshkat Date: Wed, 7 Dec 2022 00:40:14 -0500 Subject: [PATCH 01/15] Added support for default value for padding of paddedbox and fixed incorrect location. Added unit test for change. --- lib/matplotlib/offsetbox.py | 15 ++++++++++++--- lib/matplotlib/tests/test_offsetbox.py | 12 +++++++++++- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/lib/matplotlib/offsetbox.py b/lib/matplotlib/offsetbox.py index 059a6c8ae316..d659f512ef16 100644 --- a/lib/matplotlib/offsetbox.py +++ b/lib/matplotlib/offsetbox.py @@ -369,12 +369,12 @@ def draw(self, renderer): class PackerBase(OffsetBox): - def __init__(self, pad=None, sep=None, width=None, height=None, + def __init__(self, pad=0., sep=None, width=None, height=None, align="baseline", mode="fixed", children=None): """ Parameters ---------- - pad : float, optional + pad : float, required The boundary padding in points. sep : float, optional @@ -502,7 +502,7 @@ 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 ---------- @@ -531,6 +531,15 @@ def __init__(self, child, pad=None, draw_frame=False, patch_attrs=None): if patch_attrs is not None: self.patch.update(patch_attrs) + def get_offset(self, width, height, xdescent, ydescent, renderer): + # docstring inherited + bbox = Bbox.from_bounds(0, 0, width, height) + pad = (self._children[0].pad + * renderer.points_to_pixels(self._children[0].prop.get_size_in_points())) + bbox_to_anchor = self._children[0].get_bbox_to_anchor() + x0, y0 = _get_anchored_bbox(self._children[0].loc, bbox, bbox_to_anchor, pad) + return x0 + xdescent, y0 + ydescent + def get_extent_offsets(self, renderer): # docstring inherited. dpicor = renderer.points_to_pixels(1.) diff --git a/lib/matplotlib/tests/test_offsetbox.py b/lib/matplotlib/tests/test_offsetbox.py index 9f6a9be1fd0c..7057f2f24846 100644 --- a/lib/matplotlib/tests/test_offsetbox.py +++ b/lib/matplotlib/tests/test_offsetbox.py @@ -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) @@ -378,3 +378,13 @@ 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(): + fig, ax = plt.subplots() + + at = AnchoredText("foo", 'upper left') + try: + pb = PaddedBox(at, patch_attrs={'facecolor' : 'r'}, draw_frame=True) + except: + raise Exception("incorrect default value") \ No newline at end of file From 58dc0d6fd253cef46e5b1b9c121ebadb86b840b4 Mon Sep 17 00:00:00 2001 From: Ali Meshkat Date: Wed, 7 Dec 2022 00:45:36 -0500 Subject: [PATCH 02/15] ran flake on the changes for compliance --- lib/matplotlib/offsetbox.py | 6 ++++-- lib/matplotlib/tests/test_offsetbox.py | 4 ++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/matplotlib/offsetbox.py b/lib/matplotlib/offsetbox.py index d659f512ef16..d04662abf8e2 100644 --- a/lib/matplotlib/offsetbox.py +++ b/lib/matplotlib/offsetbox.py @@ -535,9 +535,11 @@ def get_offset(self, width, height, xdescent, ydescent, renderer): # docstring inherited bbox = Bbox.from_bounds(0, 0, width, height) pad = (self._children[0].pad - * renderer.points_to_pixels(self._children[0].prop.get_size_in_points())) + * renderer.points_to_pixels( + self._children[0].prop.get_size_in_points())) bbox_to_anchor = self._children[0].get_bbox_to_anchor() - x0, y0 = _get_anchored_bbox(self._children[0].loc, bbox, bbox_to_anchor, pad) + x0, y0 = _get_anchored_bbox( + self._children[0].loc, bbox, bbox_to_anchor, pad) return x0 + xdescent, y0 + ydescent def get_extent_offsets(self, renderer): diff --git a/lib/matplotlib/tests/test_offsetbox.py b/lib/matplotlib/tests/test_offsetbox.py index 7057f2f24846..32fa894f56e4 100644 --- a/lib/matplotlib/tests/test_offsetbox.py +++ b/lib/matplotlib/tests/test_offsetbox.py @@ -385,6 +385,6 @@ def test_paddedbox(): at = AnchoredText("foo", 'upper left') try: - pb = PaddedBox(at, patch_attrs={'facecolor' : 'r'}, draw_frame=True) + pb = PaddedBox(at, patch_attrs={'facecolor': 'r'}, draw_frame=True) except: - raise Exception("incorrect default value") \ No newline at end of file + raise Exception("incorrect default value") From 895c8610d1e150e7ad2284767f6ed1e30b7e6bc4 Mon Sep 17 00:00:00 2001 From: Skhaki18 Date: Wed, 7 Dec 2022 03:30:12 -0500 Subject: [PATCH 03/15] Update offsetbox.py with requested doc change and default sep --- lib/matplotlib/offsetbox.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/offsetbox.py b/lib/matplotlib/offsetbox.py index d04662abf8e2..db94c6f91265 100644 --- a/lib/matplotlib/offsetbox.py +++ b/lib/matplotlib/offsetbox.py @@ -369,12 +369,12 @@ def draw(self, renderer): class PackerBase(OffsetBox): - def __init__(self, pad=0., 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, required + pad : float, optional The boundary padding in points. sep : float, optional From 5b3397e6761906ab3487870f05490fdc57a43792 Mon Sep 17 00:00:00 2001 From: Skhaki18 Date: Wed, 7 Dec 2022 03:32:33 -0500 Subject: [PATCH 04/15] Added requested change for test_paddedbox --- lib/matplotlib/tests/test_offsetbox.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/lib/matplotlib/tests/test_offsetbox.py b/lib/matplotlib/tests/test_offsetbox.py index 32fa894f56e4..1a70d718c8b1 100644 --- a/lib/matplotlib/tests/test_offsetbox.py +++ b/lib/matplotlib/tests/test_offsetbox.py @@ -381,10 +381,7 @@ def test_packers(align): def test_paddedbox(): + # smoke test for correct default value though fig, ax = plt.subplots() - at = AnchoredText("foo", 'upper left') - try: - pb = PaddedBox(at, patch_attrs={'facecolor': 'r'}, draw_frame=True) - except: - raise Exception("incorrect default value") + pb = PaddedBox(at, patch_attrs={'facecolor': 'r'}, draw_frame=True) From 8b8b539f1eba490d0cb515ab0e8de35964ecb01d Mon Sep 17 00:00:00 2001 From: Krutarth Patel Date: Fri, 9 Dec 2022 01:38:10 -0500 Subject: [PATCH 05/15] updated to default values --- lib/matplotlib/offsetbox.py | 23 ++++++----------------- 1 file changed, 6 insertions(+), 17 deletions(-) diff --git a/lib/matplotlib/offsetbox.py b/lib/matplotlib/offsetbox.py index db94c6f91265..9577a3dfe8a1 100644 --- a/lib/matplotlib/offsetbox.py +++ b/lib/matplotlib/offsetbox.py @@ -369,7 +369,7 @@ def draw(self, renderer): class PackerBase(OffsetBox): - def __init__(self, pad=0., sep=0., width=None, height=None, + def __init__(self, pad=0, sep=0, width=None, height=None, align="baseline", mode="fixed", children=None): """ Parameters @@ -502,7 +502,7 @@ class PaddedBox(OffsetBox): """ @_api.make_keyword_only("3.6", name="draw_frame") - def __init__(self, child, pad=0., draw_frame=False, patch_attrs=None): + def __init__(self, child, pad=0, draw_frame=False, patch_attrs=None): """ Parameters ---------- @@ -531,17 +531,6 @@ def __init__(self, child, pad=0., draw_frame=False, patch_attrs=None): if patch_attrs is not None: self.patch.update(patch_attrs) - def get_offset(self, width, height, xdescent, ydescent, renderer): - # docstring inherited - bbox = Bbox.from_bounds(0, 0, width, height) - pad = (self._children[0].pad - * renderer.points_to_pixels( - self._children[0].prop.get_size_in_points())) - bbox_to_anchor = self._children[0].get_bbox_to_anchor() - x0, y0 = _get_anchored_bbox( - self._children[0].loc, bbox, bbox_to_anchor, pad) - return x0 + xdescent, y0 + ydescent - def get_extent_offsets(self, renderer): # docstring inherited. dpicor = renderer.points_to_pixels(1.) @@ -1253,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. From a1ba0cd884cc5572875c213a9ecf704c3ffb500a Mon Sep 17 00:00:00 2001 From: Krutarth Patel Date: Mon, 12 Dec 2022 14:33:42 -0500 Subject: [PATCH 06/15] Update lib/matplotlib/offsetbox.py Co-authored-by: Oscar Gustafsson --- lib/matplotlib/offsetbox.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/offsetbox.py b/lib/matplotlib/offsetbox.py index 9577a3dfe8a1..a25fb8169593 100644 --- a/lib/matplotlib/offsetbox.py +++ b/lib/matplotlib/offsetbox.py @@ -502,7 +502,7 @@ class PaddedBox(OffsetBox): """ @_api.make_keyword_only("3.6", name="draw_frame") - def __init__(self, child, pad=0, draw_frame=False, patch_attrs=None): + def __init__(self, child, pad=0., draw_frame=False, patch_attrs=None): """ Parameters ---------- From ca9bc4a802260dd91119377f0aef52ad9c3571a9 Mon Sep 17 00:00:00 2001 From: Krutarth Patel Date: Mon, 12 Dec 2022 14:33:55 -0500 Subject: [PATCH 07/15] Update lib/matplotlib/tests/test_offsetbox.py Co-authored-by: Oscar Gustafsson --- lib/matplotlib/tests/test_offsetbox.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/tests/test_offsetbox.py b/lib/matplotlib/tests/test_offsetbox.py index 1a70d718c8b1..de8edc65d020 100644 --- a/lib/matplotlib/tests/test_offsetbox.py +++ b/lib/matplotlib/tests/test_offsetbox.py @@ -381,7 +381,7 @@ def test_packers(align): def test_paddedbox(): - # smoke test for correct default value though + # smoke test for correct default value fig, ax = plt.subplots() at = AnchoredText("foo", 'upper left') pb = PaddedBox(at, patch_attrs={'facecolor': 'r'}, draw_frame=True) From 0f1f1829878f4ec490fb34f5f4a9ab5b902aaf22 Mon Sep 17 00:00:00 2001 From: Krutarth Patel Date: Mon, 12 Dec 2022 14:34:06 -0500 Subject: [PATCH 08/15] Update lib/matplotlib/offsetbox.py Co-authored-by: Oscar Gustafsson --- lib/matplotlib/offsetbox.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/offsetbox.py b/lib/matplotlib/offsetbox.py index a25fb8169593..68f77ded5163 100644 --- a/lib/matplotlib/offsetbox.py +++ b/lib/matplotlib/offsetbox.py @@ -369,7 +369,7 @@ def draw(self, renderer): class PackerBase(OffsetBox): - def __init__(self, pad=0, sep=0, width=None, height=None, + def __init__(self, pad=0., sep=0., width=None, height=None, align="baseline", mode="fixed", children=None): """ Parameters From 4d1272cf33c2272070547e36ce17369c0c8f8408 Mon Sep 17 00:00:00 2001 From: Krutarth Patel Date: Mon, 12 Dec 2022 14:47:51 -0500 Subject: [PATCH 09/15] updated doc strings --- lib/matplotlib/offsetbox.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/matplotlib/offsetbox.py b/lib/matplotlib/offsetbox.py index 68f77ded5163..1d644e9dc93c 100644 --- a/lib/matplotlib/offsetbox.py +++ b/lib/matplotlib/offsetbox.py @@ -374,10 +374,10 @@ def __init__(self, pad=0., sep=0., width=None, height=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 @@ -508,7 +508,7 @@ def __init__(self, child, pad=0., draw_frame=False, patch_attrs=None): ---------- 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. From c8c1f87e33be4c314fa802c84b0e76119bc41da9 Mon Sep 17 00:00:00 2001 From: Krutarth Patel Date: Mon, 12 Dec 2022 14:51:57 -0500 Subject: [PATCH 10/15] added smoke test for packer --- lib/matplotlib/tests/test_offsetbox.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/tests/test_offsetbox.py b/lib/matplotlib/tests/test_offsetbox.py index de8edc65d020..448289e784ac 100644 --- a/lib/matplotlib/tests/test_offsetbox.py +++ b/lib/matplotlib/tests/test_offsetbox.py @@ -380,8 +380,21 @@ def test_packers(align): assert_allclose([(x_height, 0), (0, -y2)], offset_pairs) -def test_paddedbox(): +def test_paddedbox_packer(): # smoke test 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() + + fig = plt.figure(dpi=72) + x1, y1 = 10, 30 + x2, y2 = 20, 60 + r1 = DrawingArea(x1, y1) + r2 = DrawingArea(x2, y2) + + hpacker = HPacker(children=[r1, r2], align=align) + vpacker = VPacker(children=[r1, r2], align=align) + renderer = fig.canvas.get_renderer() + From f09389dfe6e6673585f2af25396f3247205b22e4 Mon Sep 17 00:00:00 2001 From: Krutarth Patel Date: Mon, 12 Dec 2022 15:01:22 -0500 Subject: [PATCH 11/15] Update test_offsetbox.py --- lib/matplotlib/tests/test_offsetbox.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/matplotlib/tests/test_offsetbox.py b/lib/matplotlib/tests/test_offsetbox.py index 448289e784ac..36acb4c64f81 100644 --- a/lib/matplotlib/tests/test_offsetbox.py +++ b/lib/matplotlib/tests/test_offsetbox.py @@ -364,7 +364,7 @@ def test_packers(align): y_height = (y2 - y1) / 2 # x-offsets, y-offsets assert_allclose([(0, y_height), (x1, 0)], offset_pairs) - + # VPacker *extents, offset_pairs = vpacker.get_extent_offsets(renderer) # width, height, xdescent, ydescent @@ -379,15 +379,16 @@ def test_packers(align): # x-offsets, y-offsets assert_allclose([(x_height, 0), (0, -y2)], offset_pairs) - -def test_paddedbox_packer(): +@pytest.mark.parametrize("align", ["baseline", "bottom", "top", + "left", "right", "center"]) +def test_paddedbox_packer(align): # smoke test 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() - + fig = plt.figure(dpi=72) x1, y1 = 10, 30 x2, y2 = 20, 60 @@ -397,4 +398,3 @@ def test_paddedbox_packer(): hpacker = HPacker(children=[r1, r2], align=align) vpacker = VPacker(children=[r1, r2], align=align) renderer = fig.canvas.get_renderer() - From ad9fc5fcc2809b43492cf7ac88d05a31be9f6b50 Mon Sep 17 00:00:00 2001 From: Krutarth Patel Date: Mon, 12 Dec 2022 15:03:53 -0500 Subject: [PATCH 12/15] Update test_offsetbox.py --- lib/matplotlib/tests/test_offsetbox.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/tests/test_offsetbox.py b/lib/matplotlib/tests/test_offsetbox.py index 36acb4c64f81..5af7f808e128 100644 --- a/lib/matplotlib/tests/test_offsetbox.py +++ b/lib/matplotlib/tests/test_offsetbox.py @@ -364,7 +364,7 @@ def test_packers(align): y_height = (y2 - y1) / 2 # x-offsets, y-offsets assert_allclose([(0, y_height), (x1, 0)], offset_pairs) - + # VPacker *extents, offset_pairs = vpacker.get_extent_offsets(renderer) # width, height, xdescent, ydescent @@ -379,6 +379,7 @@ def test_packers(align): # x-offsets, y-offsets assert_allclose([(x_height, 0), (0, -y2)], offset_pairs) + @pytest.mark.parametrize("align", ["baseline", "bottom", "top", "left", "right", "center"]) def test_paddedbox_packer(align): From 8009e092e01ecb5104772f7c79e5efa1696308bb Mon Sep 17 00:00:00 2001 From: Skhaki18 Date: Tue, 13 Dec 2022 22:09:39 -0500 Subject: [PATCH 13/15] Update test_offsetbox.py --- lib/matplotlib/tests/test_offsetbox.py | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/lib/matplotlib/tests/test_offsetbox.py b/lib/matplotlib/tests/test_offsetbox.py index 5af7f808e128..3759eea35589 100644 --- a/lib/matplotlib/tests/test_offsetbox.py +++ b/lib/matplotlib/tests/test_offsetbox.py @@ -349,6 +349,11 @@ def test_packers(align): hpacker = HPacker(children=[r1, r2], pad=0, sep=0, align=align) vpacker = VPacker(children=[r1, r2], pad=0, sep=0, align=align) + + #smoke testing default values + hpacker = HPacker(children=[r1, r2], align=align) + vpacker = VPacker(children=[r1, r2], align=align) + renderer = fig.canvas.get_renderer() # HPacker @@ -380,22 +385,10 @@ def test_packers(align): assert_allclose([(x_height, 0), (0, -y2)], offset_pairs) -@pytest.mark.parametrize("align", ["baseline", "bottom", "top", - "left", "right", "center"]) -def test_paddedbox_packer(align): - # smoke test for correct default value +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) ax.add_artist(pb) fig.draw_without_rendering() - - fig = plt.figure(dpi=72) - x1, y1 = 10, 30 - x2, y2 = 20, 60 - r1 = DrawingArea(x1, y1) - r2 = DrawingArea(x2, y2) - - hpacker = HPacker(children=[r1, r2], align=align) - vpacker = VPacker(children=[r1, r2], align=align) - renderer = fig.canvas.get_renderer() From bfbb4b00fa046ac0ba8db0ba44df13010e6922e7 Mon Sep 17 00:00:00 2001 From: Skhaki18 Date: Tue, 13 Dec 2022 22:11:36 -0500 Subject: [PATCH 14/15] Update test_offsetbox.py --- lib/matplotlib/tests/test_offsetbox.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/tests/test_offsetbox.py b/lib/matplotlib/tests/test_offsetbox.py index 3759eea35589..5febf0bf8f1c 100644 --- a/lib/matplotlib/tests/test_offsetbox.py +++ b/lib/matplotlib/tests/test_offsetbox.py @@ -350,7 +350,7 @@ def test_packers(align): hpacker = HPacker(children=[r1, r2], pad=0, sep=0, align=align) vpacker = VPacker(children=[r1, r2], pad=0, sep=0, align=align) - #smoke testing default values + # smoke testing default values hpacker = HPacker(children=[r1, r2], align=align) vpacker = VPacker(children=[r1, r2], align=align) From cd6ecaf50518d4e0e134f0ad1388ea5ac7df9417 Mon Sep 17 00:00:00 2001 From: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> Date: Wed, 14 Dec 2022 17:46:01 +0100 Subject: [PATCH 15/15] Update lib/matplotlib/tests/test_offsetbox.py --- lib/matplotlib/tests/test_offsetbox.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/lib/matplotlib/tests/test_offsetbox.py b/lib/matplotlib/tests/test_offsetbox.py index 5febf0bf8f1c..5785cf5affe0 100644 --- a/lib/matplotlib/tests/test_offsetbox.py +++ b/lib/matplotlib/tests/test_offsetbox.py @@ -347,10 +347,6 @@ 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) - - # smoke testing default values hpacker = HPacker(children=[r1, r2], align=align) vpacker = VPacker(children=[r1, r2], align=align)