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

Skip to content

Commit 3f42ea8

Browse files
FIX: avoid applying dashed patterns to zero-width lines and patches
1 parent 32805b1 commit 3f42ea8

4 files changed

Lines changed: 30 additions & 6 deletions

File tree

lib/matplotlib/lines.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -798,8 +798,11 @@ def draw(self, renderer):
798798
if self.get_sketch_params() is not None:
799799
gc.set_sketch_params(*self.get_sketch_params())
800800

801-
# We first draw a path within the gaps if needed.
802-
if self.is_dashed() and self._gapcolor is not None:
801+
# We first draw a path within the gaps if needed, but only for
802+
# visible dashed lines; zero-width lines would otherwise yield
803+
# all-zero dashes.
804+
if (self._linewidth > 0 and self.is_dashed()
805+
and self._gapcolor is not None):
803806
lc_rgba = mcolors.to_rgba(self._gapcolor, self._alpha)
804807
gc.set_foreground(lc_rgba, isRGBA=True)
805808

@@ -812,7 +815,10 @@ def draw(self, renderer):
812815
lc_rgba = mcolors.to_rgba(self._color, self._alpha)
813816
gc.set_foreground(lc_rgba, isRGBA=True)
814817

815-
gc.set_dashes(*self._dash_pattern)
818+
if self._linewidth > 0:
819+
gc.set_dashes(*self._dash_pattern)
820+
else:
821+
gc.set_dashes(0, None)
816822
renderer.draw_path(gc, tpath, affine.frozen())
817823
gc.restore()
818824

lib/matplotlib/patches.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -709,8 +709,9 @@ def _draw_paths_with_artist_properties(
709709
from matplotlib.patheffects import PathEffectRenderer
710710
renderer = PathEffectRenderer(self.get_path_effects(), renderer)
711711

712-
# Draw the gaps first if gapcolor is set
713-
if self._has_dashed_edge() and self._gapcolor is not None:
712+
# We first draw a path within the gaps if needed, but only for visible
713+
# dashed edges; zero-width edges would otherwise yield all-zero dashes.
714+
if lw > 0 and self._has_dashed_edge() and self._gapcolor is not None:
714715
gc.set_foreground(self._gapcolor, isRGBA=True)
715716
offset_gaps, gaps = mlines._get_inverse_dash_pattern(
716717
*self._dash_pattern)
@@ -720,7 +721,10 @@ def _draw_paths_with_artist_properties(
720721

721722
# Draw the main edge
722723
gc.set_foreground(self._edgecolor, isRGBA=True)
723-
gc.set_dashes(*self._dash_pattern)
724+
if lw > 0:
725+
gc.set_dashes(*self._dash_pattern)
726+
else:
727+
gc.set_dashes(0, None)
724728
for draw_path_args in draw_path_args_list:
725729
renderer.draw_path(gc, *draw_path_args)
726730

lib/matplotlib/tests/test_axes.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8084,6 +8084,14 @@ def test_twinning_default_axes_class():
80848084
def test_zero_linewidth():
80858085
# Check that setting a zero linewidth doesn't error
80868086
plt.plot([0, 1], [0, 1], ls='--', lw=0)
8087+
plt.gcf().canvas.draw()
8088+
8089+
8090+
@mpl.style.context('mpl20')
8091+
def test_stairs_fill_zero_linewidth():
8092+
fig, ax = plt.subplots()
8093+
ax.stairs([1, 2, 3, 4], [1, 2, 3, 4, 5], fill=True, ls='--')
8094+
fig.canvas.draw()
80878095

80888096

80898097
def test_empty_errorbar_legend():

lib/matplotlib/tests/test_patches.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -907,6 +907,12 @@ def test_default_linestyle():
907907
assert patch.get_linestyle() == 'solid'
908908

909909

910+
def test_patch_zero_linewidth_dashed_draw():
911+
fig, ax = plt.subplots()
912+
ax.add_patch(Rectangle((0, 0), 1, 1, fill=False, linewidth=0, linestyle='--'))
913+
fig.canvas.draw()
914+
915+
910916
def test_default_capstyle():
911917
patch = Patch()
912918
assert patch.get_capstyle() == 'butt'

0 commit comments

Comments
 (0)