@@ -272,6 +272,7 @@ def __init__(self, xdata, ydata,
272
272
linewidth = None , # all Nones default to rc
273
273
linestyle = None ,
274
274
color = None ,
275
+ second_color = None ,
275
276
marker = None ,
276
277
markersize = None ,
277
278
markeredgewidth = None ,
@@ -364,6 +365,9 @@ def __init__(self, xdata, ydata,
364
365
else :
365
366
self ._marker = marker
366
367
368
+ self ._second_color = None
369
+ self .set_second_color (second_color )
370
+
367
371
self ._markevery = None
368
372
self ._markersize = None
369
373
self ._antialiased = None
@@ -754,9 +758,6 @@ def draw(self, renderer):
754
758
self ._set_gc_clip (gc )
755
759
gc .set_url (self .get_url ())
756
760
757
- lc_rgba = mcolors .to_rgba (self ._color , self ._alpha )
758
- gc .set_foreground (lc_rgba , isRGBA = True )
759
-
760
761
gc .set_antialiased (self ._antialiased )
761
762
gc .set_linewidth (self ._linewidth )
762
763
@@ -772,6 +773,26 @@ def draw(self, renderer):
772
773
if self .get_sketch_params () is not None :
773
774
gc .set_sketch_params (* self .get_sketch_params ())
774
775
776
+ # We first draw a path within the gaps if needed.
777
+ if self .is_dashed () and self ._second_color is not None :
778
+ lc_rgba = mcolors .to_rgba (self ._second_color , 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
+
775
796
gc .set_dashes (* self ._dash_pattern )
776
797
renderer .draw_path (gc , tpath , affine .frozen ())
777
798
gc .restore ()
@@ -959,6 +980,14 @@ def get_markersize(self):
959
980
"""
960
981
return self ._markersize
961
982
983
+ def get_second_color (self ):
984
+ """
985
+ Return the line second_color.
986
+
987
+ See also `~.Line2D.set_second_color`.
988
+ """
989
+ return self ._second_color
990
+
962
991
def get_data (self , orig = True ):
963
992
"""
964
993
Return the line data as an ``(xdata, ydata)`` pair.
@@ -1213,6 +1242,28 @@ def set_markersize(self, sz):
1213
1242
self .stale = True
1214
1243
self ._markersize = sz
1215
1244
1245
+ def set_second_color (self , second_color ):
1246
+ """
1247
+ Set a second color to fill the gaps in the dashed line style.
1248
+
1249
+ .. note::
1250
+
1251
+ Striped lines are created by drawing two interleaved dashed lines.
1252
+ There can be overlaps between those two, which may result in
1253
+ artifacts when using transparency.
1254
+
1255
+ This functionality is experimental and may change.
1256
+
1257
+ Parameters
1258
+ ----------
1259
+ second_color : color or None
1260
+ The second color. If None, the gaps are unfilled.
1261
+ """
1262
+ if second_color is not None :
1263
+ mcolors ._check_color_like (color = second_color )
1264
+ self ._second_color = second_color
1265
+ self .stale = True
1266
+
1216
1267
def set_xdata (self , x ):
1217
1268
"""
1218
1269
Set the data array for x.
@@ -1247,6 +1298,9 @@ def set_dashes(self, seq):
1247
1298
For example, (5, 2, 1, 2) describes a sequence of 5 point and 1 point
1248
1299
dashes separated by 2 point spaces.
1249
1300
1301
+ See also `~.Line2D.set_second_color`, which allows those spaces to be
1302
+ filled with a color.
1303
+
1250
1304
Parameters
1251
1305
----------
1252
1306
seq : sequence of floats (on/off ink in points) or (None, None)
@@ -1264,6 +1318,7 @@ def update_from(self, other):
1264
1318
self ._linestyle = other ._linestyle
1265
1319
self ._linewidth = other ._linewidth
1266
1320
self ._color = other ._color
1321
+ self ._second_color = other ._second_color
1267
1322
self ._markersize = other ._markersize
1268
1323
self ._markerfacecolor = other ._markerfacecolor
1269
1324
self ._markerfacecoloralt = other ._markerfacecoloralt
0 commit comments