Skip to content

Commit 4eed106

Browse files
authored
Merge pull request matplotlib#31307 from francisayyad03/main
FIX: avoid applying dashed patterns to zero-width lines and patches
2 parents 5258412 + aafeacd commit 4eed106

5 files changed

Lines changed: 38 additions & 9 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: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8081,9 +8081,13 @@ def test_twinning_default_axes_class():
80818081
assert type(twiny) is Axes
80828082

80838083

8084-
def test_zero_linewidth():
8085-
# Check that setting a zero linewidth doesn't error
8086-
plt.plot([0, 1], [0, 1], ls='--', lw=0)
8084+
@mpl.style.context('mpl20')
8085+
@check_figures_equal()
8086+
def test_stairs_fill_zero_linewidth(fig_test, fig_ref):
8087+
fig_test.subplots().stairs(
8088+
[1, 2, 3, 4], [1, 2, 3, 4, 5], fill=True, ls='--')
8089+
fig_ref.subplots().stairs(
8090+
[1, 2, 3, 4], [1, 2, 3, 4, 5], fill=True, ls='-')
80878091

80888092

80898093
def test_empty_errorbar_legend():

lib/matplotlib/tests/test_lines.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,13 @@ def test_valid_linestyles():
9292
line.set_linestyle('aardvark')
9393

9494

95+
@mpl.style.context('mpl20')
96+
def test_zero_linewidth_dashed_uses_solid_gc_dashes():
97+
fig, ax = plt.subplots()
98+
ax.plot([0, 1], [0, 1], ls='--', lw=0)
99+
fig.draw_without_rendering()
100+
101+
95102
@image_comparison(['drawstyle_variants.png'], remove_text=True,
96103
tol=0 if platform.machine() == 'x86_64' else 0.03)
97104
def test_drawstyle_variants():

lib/matplotlib/tests/test_patches.py

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

909909

910+
@mpl.style.context('mpl20')
911+
def test_patch_zero_linewidth_dashed_uses_solid_gc_dashes():
912+
fig, ax = plt.subplots()
913+
ax.add_patch(Rectangle(
914+
(0, 0), 1, 1, fill=False, linewidth=0, linestyle='--'))
915+
fig.draw_without_rendering()
916+
917+
910918
def test_default_capstyle():
911919
patch = Patch()
912920
assert patch.get_capstyle() == 'butt'

0 commit comments

Comments
 (0)