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..7be55858f70f --- /dev/null +++ b/doc/users/next_whats_new/fix_dash_offset_Patch.rst @@ -0,0 +1,5 @@ +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. diff --git a/lib/matplotlib/patches.py b/lib/matplotlib/patches.py index e062249589e2..8a8b2e2db509 100644 --- a/lib/matplotlib/patches.py +++ b/lib/matplotlib/patches.py @@ -586,9 +586,8 @@ 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])), \ + + 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() diff --git a/lib/matplotlib/tests/test_patches.py b/lib/matplotlib/tests/test_patches.py index 7064d0dd3b19..6e7db4eebbcf 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.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) + + # 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. + + 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) + + 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.)