9
9
line segments).
10
10
"""
11
11
12
+ import itertools
12
13
import math
13
14
from numbers import Number
14
15
import warnings
@@ -163,6 +164,9 @@ def __init__(self,
163
164
# list of unbroadcast/scaled linewidths
164
165
self ._us_lw = [0 ]
165
166
self ._linewidths = [0 ]
167
+
168
+ self ._gapcolor = None # Currently only used by LineCollection.
169
+
166
170
# Flags set by _set_mappable_flags: are colors from mapping an array?
167
171
self ._face_is_mapped = None
168
172
self ._edge_is_mapped = None
@@ -406,6 +410,17 @@ def draw(self, renderer):
406
410
gc , paths [0 ], combined_transform .frozen (),
407
411
mpath .Path (offsets ), offset_trf , tuple (facecolors [0 ]))
408
412
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
+
409
424
renderer .draw_path_collection (
410
425
gc , transform .frozen (), paths ,
411
426
self .get_transforms (), offsets , offset_trf ,
@@ -1459,6 +1474,12 @@ def _get_default_edgecolor(self):
1459
1474
def _get_default_facecolor (self ):
1460
1475
return 'none'
1461
1476
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
+
1462
1483
def set_color (self , c ):
1463
1484
"""
1464
1485
Set the edgecolor(s) of the LineCollection.
@@ -1479,6 +1500,53 @@ def get_color(self):
1479
1500
1480
1501
get_colors = get_color # for compatibility with old versions
1481
1502
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
+
1482
1550
1483
1551
class EventCollection (LineCollection ):
1484
1552
"""
0 commit comments