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

Skip to content

Commit d7ffff2

Browse files
authored
Fix dash offset bug in Patch (#23412)
* Add passed offset in draw method of Patch * Add entry to whats new about dash offset * Remove trailing spaces * Add test for dash offset in Patch * Refactor test_dash_offset_patch_draw * Correct mention of code object * Fix indentation in draw method of Patch
1 parent 29a8663 commit d7ffff2

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Fix the dash offset of the Patch class
2+
--------------------------------------
3+
Traditionally, when setting the linestyle on a `.Patch` object using a dash tuple the
4+
offset was ignored. Now the offset is passed to the draw method of Patch as expected
5+
and it can be used as it is used with Line2D objects.

lib/matplotlib/patches.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -586,9 +586,8 @@ def draw(self, renderer):
586586
# docstring inherited
587587
if not self.get_visible():
588588
return
589-
# Patch has traditionally ignored the dashoffset.
590-
with cbook._setattr_cm(
591-
self, _dash_pattern=(0, self._dash_pattern[1])), \
589+
590+
with cbook._setattr_cm(self, _dash_pattern=(self._dash_pattern)), \
592591
self._bind_draw_path_function(renderer) as draw_path:
593592
path = self.get_path()
594593
transform = self.get_transform()

lib/matplotlib/tests/test_patches.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,40 @@ def test_rotate_rect_draw(fig_test, fig_ref):
149149
assert rect_test.get_angle() == angle
150150

151151

152+
@check_figures_equal(extensions=['png'])
153+
def test_dash_offset_patch_draw(fig_test, fig_ref):
154+
ax_test = fig_test.add_subplot()
155+
ax_ref = fig_ref.add_subplot()
156+
157+
loc = (0.1, 0.1)
158+
width, height = (0.8, 0.8)
159+
rect_ref = Rectangle(loc, width, height, linewidth=3, edgecolor='b',
160+
linestyle=(0, [6, 6]))
161+
# fill the line gaps using a linestyle (0, [0, 6, 6, 0]), which is
162+
# equivalent to (6, [6, 6]) but has 0 dash offset
163+
rect_ref2 = Rectangle(loc, width, height, linewidth=3, edgecolor='r',
164+
linestyle=(0, [0, 6, 6, 0]))
165+
assert rect_ref.get_linestyle() == (0, [6, 6])
166+
assert rect_ref2.get_linestyle() == (0, [0, 6, 6, 0])
167+
168+
ax_ref.add_patch(rect_ref)
169+
ax_ref.add_patch(rect_ref2)
170+
171+
# Check that the dash offset of the rect is the same if we pass it in the
172+
# init method and if we create two rects with appropriate onoff sequence
173+
# of linestyle.
174+
175+
rect_test = Rectangle(loc, width, height, linewidth=3, edgecolor='b',
176+
linestyle=(0, [6, 6]))
177+
rect_test2 = Rectangle(loc, width, height, linewidth=3, edgecolor='r',
178+
linestyle=(6, [6, 6]))
179+
assert rect_test.get_linestyle() == (0, [6, 6])
180+
assert rect_test2.get_linestyle() == (6, [6, 6])
181+
182+
ax_test.add_patch(rect_test)
183+
ax_test.add_patch(rect_test2)
184+
185+
152186
def test_negative_rect():
153187
# These two rectangles have the same vertices, but starting from a
154188
# different point. (We also drop the last vertex, which is a duplicate.)

0 commit comments

Comments
 (0)