99line segments).
1010"""
1111
12+ import itertools
1213import math
1314from numbers import Number
1415import warnings
@@ -163,6 +164,9 @@ def __init__(self,
163164 # list of unbroadcast/scaled linewidths
164165 self ._us_lw = [0 ]
165166 self ._linewidths = [0 ]
167+
168+ self ._gapcolor = None # Currently only used by LineCollection.
169+
166170 # Flags set by _set_mappable_flags: are colors from mapping an array?
167171 self ._face_is_mapped = None
168172 self ._edge_is_mapped = None
@@ -406,6 +410,17 @@ def draw(self, renderer):
406410 gc , paths [0 ], combined_transform .frozen (),
407411 mpath .Path (offsets ), offset_trf , tuple (facecolors [0 ]))
408412 else :
413+ if self ._gapcolor is not None :
414+ # First draw paths within the gaps.
415+ ipaths , ilinestyles = self ._get_inverse_paths_linestyles ()
416+ renderer .draw_path_collection (
417+ gc , transform .frozen (), ipaths ,
418+ self .get_transforms (), offsets , offset_trf ,
419+ [mcolors .to_rgba ("none" )], self ._gapcolor ,
420+ self ._linewidths , ilinestyles ,
421+ self ._antialiaseds , self ._urls ,
422+ "screen" )
423+
409424 renderer .draw_path_collection (
410425 gc , transform .frozen (), paths ,
411426 self .get_transforms (), offsets , offset_trf ,
@@ -1459,6 +1474,12 @@ def _get_default_edgecolor(self):
14591474 def _get_default_facecolor (self ):
14601475 return 'none'
14611476
1477+ def set_alpha (self , alpha ):
1478+ # docstring inherited
1479+ super ().set_alpha (alpha )
1480+ if self ._gapcolor is not None :
1481+ self .set_gapcolor (self ._original_gapcolor )
1482+
14621483 def set_color (self , c ):
14631484 """
14641485 Set the edgecolor(s) of the LineCollection.
@@ -1479,6 +1500,53 @@ def get_color(self):
14791500
14801501 get_colors = get_color # for compatibility with old versions
14811502
1503+ def set_gapcolor (self , gapcolor ):
1504+ """
1505+ Set a color to fill the gaps in the dashed line style.
1506+
1507+ .. note::
1508+
1509+ Striped lines are created by drawing two interleaved dashed lines.
1510+ There can be overlaps between those two, which may result in
1511+ artifacts when using transparency.
1512+
1513+ This functionality is experimental and may change.
1514+
1515+ Parameters
1516+ ----------
1517+ gapcolor : color or list of colors or None
1518+ The color with which to fill the gaps. If None, the gaps are
1519+ unfilled.
1520+ """
1521+ self ._original_gapcolor = gapcolor
1522+ self ._set_gapcolor (gapcolor )
1523+
1524+ def _set_gapcolor (self , gapcolor ):
1525+ if gapcolor is not None :
1526+ gapcolor = mcolors .to_rgba_array (gapcolor , self ._alpha )
1527+ self ._gapcolor = gapcolor
1528+ self .stale = True
1529+
1530+ def get_gapcolor (self ):
1531+ return self ._gapcolor
1532+
1533+ def _get_inverse_paths_linestyles (self ):
1534+ """
1535+ Returns the path and pattern for the gaps in the non-solid lines.
1536+
1537+ This path and pattern is the inverse of the path and pattern used to
1538+ construct the non-solid lines. For solid lines, we set the inverse path
1539+ to nans to prevent drawing an inverse line.
1540+ """
1541+ path_patterns = [
1542+ (mpath .Path (np .full ((1 , 2 ), np .nan )), ls )
1543+ if ls == (0 , None ) else
1544+ (path , mlines ._get_inverse_dash_pattern (* ls ))
1545+ for (path , ls ) in
1546+ zip (self ._paths , itertools .cycle (self ._linestyles ))]
1547+
1548+ return zip (* path_patterns )
1549+
14821550
14831551class EventCollection (LineCollection ):
14841552 """
0 commit comments