From 43bff962d36f48a61254b7105665a24709f4e32f Mon Sep 17 00:00:00 2001 From: Anna Mastori Date: Mon, 11 Jul 2022 04:09:19 +0300 Subject: [PATCH 1/7] Add passed offset in draw method of Patch --- lib/matplotlib/patches.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/patches.py b/lib/matplotlib/patches.py index e062249589e2..60deb23ff205 100644 --- a/lib/matplotlib/patches.py +++ b/lib/matplotlib/patches.py @@ -586,9 +586,9 @@ def draw(self, renderer): # docstring inherited if not self.get_visible(): return - # Patch has traditionally ignored the dashoffset. + with cbook._setattr_cm( - self, _dash_pattern=(0, self._dash_pattern[1])), \ + self, _dash_pattern=(self._dash_pattern)), \ self._bind_draw_path_function(renderer) as draw_path: path = self.get_path() transform = self.get_transform() From ac4ba14d2f3f02ac1342e81ca48cb93e967ac583 Mon Sep 17 00:00:00 2001 From: Anna Mastori Date: Mon, 11 Jul 2022 22:12:10 +0300 Subject: [PATCH 2/7] Add entry to whats new about dash offset --- doc/users/next_whats_new/fix_dash_offset_Patch.rst | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 doc/users/next_whats_new/fix_dash_offset_Patch.rst diff --git a/doc/users/next_whats_new/fix_dash_offset_Patch.rst b/doc/users/next_whats_new/fix_dash_offset_Patch.rst new file mode 100644 index 000000000000..60b91e9dc19e --- /dev/null +++ b/doc/users/next_whats_new/fix_dash_offset_Patch.rst @@ -0,0 +1,6 @@ +Fix the dash offset of the Patch class +-------------------------------------- +Traditionally, when setting the linestyle on a patch object using a dash tuple the +offset was ignored. Now the offset is passed to the draw method of Patch as expected +and it can be used as it is used with Line2D objects. + From 18bf3b1acec2c83d0fa49acaaacb87f2d15ccb6f Mon Sep 17 00:00:00 2001 From: Anna Mastori Date: Mon, 11 Jul 2022 22:19:12 +0300 Subject: [PATCH 3/7] Remove trailing spaces --- doc/users/next_whats_new/fix_dash_offset_Patch.rst | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/doc/users/next_whats_new/fix_dash_offset_Patch.rst b/doc/users/next_whats_new/fix_dash_offset_Patch.rst index 60b91e9dc19e..39c6b9f3d9f8 100644 --- a/doc/users/next_whats_new/fix_dash_offset_Patch.rst +++ b/doc/users/next_whats_new/fix_dash_offset_Patch.rst @@ -2,5 +2,4 @@ Fix the dash offset of the Patch class -------------------------------------- Traditionally, when setting the linestyle on a patch object using a dash tuple the offset was ignored. Now the offset is passed to the draw method of Patch as expected -and it can be used as it is used with Line2D objects. - +and it can be used as it is used with Line2D objects. From b3cdcdfd59398b32e33ef8fcf32f45b84ff94545 Mon Sep 17 00:00:00 2001 From: Anna Mastori Date: Wed, 13 Jul 2022 14:36:36 +0300 Subject: [PATCH 4/7] Add test for dash offset in Patch --- lib/matplotlib/tests/test_patches.py | 34 ++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/lib/matplotlib/tests/test_patches.py b/lib/matplotlib/tests/test_patches.py index 7064d0dd3b19..218e18c31350 100644 --- a/lib/matplotlib/tests/test_patches.py +++ b/lib/matplotlib/tests/test_patches.py @@ -149,6 +149,40 @@ def test_rotate_rect_draw(fig_test, fig_ref): assert rect_test.get_angle() == angle +@check_figures_equal(extensions=['png']) +def test_dash_offset_patch_draw(fig_test, fig_ref): + ax_test = fig_test.add_subplot() + ax_ref = fig_ref.add_subplot() + + loc = (0, 0) + width, height = (1, 1) + edgecolor = 'b' + linestyle = (0, [6, 6]) + linestyle_hacked = (0, [0, 6, 6, 0]) + rect_ref = Rectangle(loc, width, height, edgecolor=edgecolor, + linestyle=linestyle) + rect_ref2 = Rectangle(loc, width, height, edgecolor=edgecolor, + linestyle=linestyle_hacked) + ax_ref.add_patch(rect_ref) + ax_ref.add_patch(rect_ref2) + + assert rect_ref.get_linestyle() == linestyle + assert rect_ref2.get_linestyle() == linestyle_hacked + + # Check that the dash offset of the rect is the same if we pass it in the + # init method and if we create two rects with appropriate onoff sequence + # of linestyle. + linestyle_test = (6, [6, 6]) + rect_test = Rectangle(loc, width, height, edgecolor=edgecolor, + linestyle=linestyle) + rect_test2 = Rectangle(loc, width, height, edgecolor=edgecolor, + linestyle=linestyle_test) + assert rect_test.get_linestyle() == linestyle + assert rect_test2.get_linestyle() == linestyle_test + ax_test.add_patch(rect_test) + ax_test.add_patch(rect_test2) + + def test_negative_rect(): # These two rectangles have the same vertices, but starting from a # different point. (We also drop the last vertex, which is a duplicate.) From d8107945588da783143cd7886bd928f5e8e7f666 Mon Sep 17 00:00:00 2001 From: Anna Mastori Date: Fri, 5 Aug 2022 18:16:01 +0300 Subject: [PATCH 5/7] Refactor test_dash_offset_patch_draw --- lib/matplotlib/tests/test_patches.py | 38 ++++++++++++++-------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/lib/matplotlib/tests/test_patches.py b/lib/matplotlib/tests/test_patches.py index 218e18c31350..6e7db4eebbcf 100644 --- a/lib/matplotlib/tests/test_patches.py +++ b/lib/matplotlib/tests/test_patches.py @@ -154,31 +154,31 @@ def test_dash_offset_patch_draw(fig_test, fig_ref): ax_test = fig_test.add_subplot() ax_ref = fig_ref.add_subplot() - loc = (0, 0) - width, height = (1, 1) - edgecolor = 'b' - linestyle = (0, [6, 6]) - linestyle_hacked = (0, [0, 6, 6, 0]) - rect_ref = Rectangle(loc, width, height, edgecolor=edgecolor, - linestyle=linestyle) - rect_ref2 = Rectangle(loc, width, height, edgecolor=edgecolor, - linestyle=linestyle_hacked) + loc = (0.1, 0.1) + width, height = (0.8, 0.8) + rect_ref = Rectangle(loc, width, height, linewidth=3, edgecolor='b', + linestyle=(0, [6, 6])) + # fill the line gaps using a linestyle (0, [0, 6, 6, 0]), which is + # equivalent to (6, [6, 6]) but has 0 dash offset + rect_ref2 = Rectangle(loc, width, height, linewidth=3, edgecolor='r', + linestyle=(0, [0, 6, 6, 0])) + assert rect_ref.get_linestyle() == (0, [6, 6]) + assert rect_ref2.get_linestyle() == (0, [0, 6, 6, 0]) + ax_ref.add_patch(rect_ref) ax_ref.add_patch(rect_ref2) - assert rect_ref.get_linestyle() == linestyle - assert rect_ref2.get_linestyle() == linestyle_hacked - # Check that the dash offset of the rect is the same if we pass it in the # init method and if we create two rects with appropriate onoff sequence # of linestyle. - linestyle_test = (6, [6, 6]) - rect_test = Rectangle(loc, width, height, edgecolor=edgecolor, - linestyle=linestyle) - rect_test2 = Rectangle(loc, width, height, edgecolor=edgecolor, - linestyle=linestyle_test) - assert rect_test.get_linestyle() == linestyle - assert rect_test2.get_linestyle() == linestyle_test + + rect_test = Rectangle(loc, width, height, linewidth=3, edgecolor='b', + linestyle=(0, [6, 6])) + rect_test2 = Rectangle(loc, width, height, linewidth=3, edgecolor='r', + linestyle=(6, [6, 6])) + assert rect_test.get_linestyle() == (0, [6, 6]) + assert rect_test2.get_linestyle() == (6, [6, 6]) + ax_test.add_patch(rect_test) ax_test.add_patch(rect_test2) From b9edc6f39dff033911e26d0195734895bc07c1ca Mon Sep 17 00:00:00 2001 From: Anna Mastori Date: Fri, 5 Aug 2022 18:21:22 +0300 Subject: [PATCH 6/7] Correct mention of code object --- doc/users/next_whats_new/fix_dash_offset_Patch.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/users/next_whats_new/fix_dash_offset_Patch.rst b/doc/users/next_whats_new/fix_dash_offset_Patch.rst index 39c6b9f3d9f8..7be55858f70f 100644 --- a/doc/users/next_whats_new/fix_dash_offset_Patch.rst +++ b/doc/users/next_whats_new/fix_dash_offset_Patch.rst @@ -1,5 +1,5 @@ Fix the dash offset of the Patch class -------------------------------------- -Traditionally, when setting the linestyle on a patch object using a dash tuple the +Traditionally, when setting the linestyle on a `.Patch` object using a dash tuple the offset was ignored. Now the offset is passed to the draw method of Patch as expected and it can be used as it is used with Line2D objects. From 4ebe03a8d98f8a078cbd8334dce140e2ad1e1658 Mon Sep 17 00:00:00 2001 From: Anna Mastori Date: Fri, 5 Aug 2022 18:41:13 +0300 Subject: [PATCH 7/7] Fix indentation in draw method of Patch --- lib/matplotlib/patches.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/matplotlib/patches.py b/lib/matplotlib/patches.py index 60deb23ff205..8a8b2e2db509 100644 --- a/lib/matplotlib/patches.py +++ b/lib/matplotlib/patches.py @@ -587,8 +587,7 @@ def draw(self, renderer): if not self.get_visible(): return - with cbook._setattr_cm( - self, _dash_pattern=(self._dash_pattern)), \ + with cbook._setattr_cm(self, _dash_pattern=(self._dash_pattern)), \ self._bind_draw_path_function(renderer) as draw_path: path = self.get_path() transform = self.get_transform()