@@ -272,6 +272,7 @@ def __init__(self, xdata, ydata,
272272 linewidth = None , # all Nones default to rc
273273 linestyle = None ,
274274 color = None ,
275+ gapcolor = None ,
275276 marker = None ,
276277 markersize = None ,
277278 markeredgewidth = None ,
@@ -364,6 +365,9 @@ def __init__(self, xdata, ydata,
364365 else :
365366 self ._marker = marker
366367
368+ self ._gapcolor = None
369+ self .set_gapcolor (gapcolor )
370+
367371 self ._markevery = None
368372 self ._markersize = None
369373 self ._antialiased = None
@@ -754,9 +758,6 @@ def draw(self, renderer):
754758 self ._set_gc_clip (gc )
755759 gc .set_url (self .get_url ())
756760
757- lc_rgba = mcolors .to_rgba (self ._color , self ._alpha )
758- gc .set_foreground (lc_rgba , isRGBA = True )
759-
760761 gc .set_antialiased (self ._antialiased )
761762 gc .set_linewidth (self ._linewidth )
762763
@@ -772,6 +773,26 @@ def draw(self, renderer):
772773 if self .get_sketch_params () is not None :
773774 gc .set_sketch_params (* self .get_sketch_params ())
774775
776+ # We first draw a path within the gaps if needed.
777+ if self .is_dashed () and self ._gapcolor is not None :
778+ lc_rgba = mcolors .to_rgba (self ._gapcolor , self ._alpha )
779+ gc .set_foreground (lc_rgba , isRGBA = True )
780+
781+ # Define the inverse pattern by moving the last gap to the
782+ # start of the sequence.
783+ dashes = self ._dash_pattern [1 ]
784+ gaps = dashes [- 1 :] + dashes [:- 1 ]
785+ # Set the offset so that this new first segment is skipped
786+ # (see backend_bases.GraphicsContextBase.set_dashes for
787+ # offset definition).
788+ offset_gaps = self ._dash_pattern [0 ] + dashes [- 1 ]
789+
790+ gc .set_dashes (offset_gaps , gaps )
791+ renderer .draw_path (gc , tpath , affine .frozen ())
792+
793+ lc_rgba = mcolors .to_rgba (self ._color , self ._alpha )
794+ gc .set_foreground (lc_rgba , isRGBA = True )
795+
775796 gc .set_dashes (* self ._dash_pattern )
776797 renderer .draw_path (gc , tpath , affine .frozen ())
777798 gc .restore ()
@@ -876,6 +897,14 @@ def get_drawstyle(self):
876897 """
877898 return self ._drawstyle
878899
900+ def get_gapcolor (self ):
901+ """
902+ Return the line gapcolor.
903+
904+ See also `~.Line2D.set_gapcolor`.
905+ """
906+ return self ._gapcolor
907+
879908 def get_linestyle (self ):
880909 """
881910 Return the linestyle.
@@ -1066,6 +1095,29 @@ def set_drawstyle(self, drawstyle):
10661095 self ._invalidx = True
10671096 self ._drawstyle = drawstyle
10681097
1098+ def set_gapcolor (self , gapcolor ):
1099+ """
1100+ Set a color to fill the gaps in the dashed line style.
1101+
1102+ .. note::
1103+
1104+ Striped lines are created by drawing two interleaved dashed lines.
1105+ There can be overlaps between those two, which may result in
1106+ artifacts when using transparency.
1107+
1108+ This functionality is experimental and may change.
1109+
1110+ Parameters
1111+ ----------
1112+ gapcolor : color or None
1113+ The color with which to fill the gaps. If None, the gaps are
1114+ unfilled.
1115+ """
1116+ if gapcolor is not None :
1117+ mcolors ._check_color_like (color = gapcolor )
1118+ self ._gapcolor = gapcolor
1119+ self .stale = True
1120+
10691121 def set_linewidth (self , w ):
10701122 """
10711123 Set the line width in points.
@@ -1247,6 +1299,9 @@ def set_dashes(self, seq):
12471299 For example, (5, 2, 1, 2) describes a sequence of 5 point and 1 point
12481300 dashes separated by 2 point spaces.
12491301
1302+ See also `~.Line2D.set_gapcolor`, which allows those spaces to be
1303+ filled with a color.
1304+
12501305 Parameters
12511306 ----------
12521307 seq : sequence of floats (on/off ink in points) or (None, None)
@@ -1264,6 +1319,7 @@ def update_from(self, other):
12641319 self ._linestyle = other ._linestyle
12651320 self ._linewidth = other ._linewidth
12661321 self ._color = other ._color
1322+ self ._gapcolor = other ._gapcolor
12671323 self ._markersize = other ._markersize
12681324 self ._markerfacecolor = other ._markerfacecolor
12691325 self ._markerfacecoloralt = other ._markerfacecoloralt
0 commit comments