Skip to content

Fix dash offset bug in Patch #23412

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 7 commits into from
Aug 5, 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
5 changes: 5 additions & 0 deletions doc/users/next_whats_new/fix_dash_offset_Patch.rst
Original file line number Diff line number Diff line change
@@ -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.
5 changes: 2 additions & 3 deletions lib/matplotlib/patches.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
34 changes: 34 additions & 0 deletions lib/matplotlib/tests/test_patches.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.)
Expand Down